Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

Hi, I believe that all my code is running correctly and every single test that I’ve run is fully correct and displaying as it should be but for some reason it is not passing me
Can someone tell me if there is anything wrong with my code?

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    count_multiply = 0
    count_division = 0
    count_total = 0
    operator_check_length = float(-2)*len(problems)
    digits_check_list = []
    list_of_more_than_4_digits = []
    list_of_less_than_4_digits = []
    number_of_dashes_list = []    
    row1 = []
    row2 = []
    row3 = []
    row4 = []
    if len(problems) > 5:
        print('Error: Too many problems.')
    else:
        for individual_problem in problems:
            multiply_check = individual_problem.find('*')
            division_check = individual_problem.find('/')
            count_multiply += multiply_check
            count_division += division_check
            count_total = count_multiply + count_division
        if count_total != operator_check_length:
            print("Error: Operator must be '+' or '-'.")
        else:
            for individual_problem in problems:
                individual_problem_space_removed = individual_problem.replace(' ', '0')
                if individual_problem_space_removed.find('+') == -1:
                    individual_problem_digits_check = individual_problem_space_removed.replace('-','0') 
                    digits_check_list.append(individual_problem_digits_check)
                elif individual_problem_space_removed.find('-') == -1:
                    individual_problem_digits_check = individual_problem_space_removed.replace('+','0')
                    digits_check_list.append(individual_problem_digits_check)
            converted_to_digits = ''.join(digits_check_list)
            if converted_to_digits.isdigit():
                for problem in problems:
                    split_list = problem.split()
                    for individual_components in split_list:
                        if individual_components == '+' or individual_components == '-':
                            pass
                        elif individual_components.isdigit:
                            if int(individual_components) <10000:
                                list_of_less_than_4_digits.append(individual_components)
                            else:
                                list_of_more_than_4_digits.append(individual_components)
                if list_of_more_than_4_digits == []:
                    for problem in problems:
                        sub_everything = problem.split()
                        row1.append(' '*(max(len(sub_everything[0]),len(sub_everything[2]))+2-len(sub_everything[0]))+sub_everything[0]+' '*4)
                    for problem in problems:
                        sub_everything2 = problem.split()
                        row2.append(sub_everything2[1]+' '*((max(len(sub_everything2[0]),len(sub_everything2[2]))+1)-len(sub_everything2[2]))+sub_everything2[2]+' '*4)
                    for problem in problems:
                        sub_everything3 = problem.split()
                        row3.append('-'*(max(len(sub_everything3[0]),len(sub_everything3[2]))+2)+' '*4)
                    for problem in problems:
                        sub_everything4 = problem.split()
                        row4.append(' '*(max(len(sub_everything4[0]),len(sub_everything4[2]))+2-int(len(str(int(sub_everything4[0])+(int(sub_everything4[1]+'1')*int(sub_everything4[2]))))))+str(int(sub_everything4[0])+(int(sub_everything4[1]+'1')*int(sub_everything4[2])))+' '*4)                                     
                    print(''.join(row1).rstrip())
                    print(''.join(row2).rstrip())
                    print(''.join(row3).rstrip())
                    if show_answers == True:
                        print(''.join(row4))                           
                else:
                    print('Error: Numbers cannot be more than four digits.')                               
            else:
                print('Error: Numbers must only contain digits.')
    return problems

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Hey there,

Please update the message to include your code. The code was too long to be automatically inserted by the help button.

When you enter a code, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

Hi thanks I posted it with the code now

I see some print statements and no return statements in some branching of your logic. Are you sure it is the right call?

Should I modify all the existing ‘print()’ calls so that it becomes ‘return print()’ ?

what would the function return in that case? print prints something to the terminal, but what does it outputs?

Ohh thanks I changed it to just return instead, but I think I am having a little bit of trouble combining all the newlines without having a new return

                    return(''.join(row1).rstrip())
                    return(''.join(row2).rstrip())
                    return(''.join(row3).rstrip())
                    if show_answers == True:
                        return(f"{''.join(row4)}")    

a return stops the function from executing, so the things after the first return, it’s like they don’t exist

Thanks I did a little fix into it becoming this:

 if show_answers is True:
                        return('\n'.join([''.join(row1).rstrip(), ''.join(row2).rstrip(), ''.join(row3).rstrip(),''.join(row4)]))
                    else:
                        return('\n'.join([''.join(row1).rstrip(), ''.join(row2).rstrip(), ''.join(row3).rstrip()]))
                  

and now it passes all the tests except for the last 2 but I am unsure what is causing that

if you share your updated code I can take a look

def arithmetic_arranger(problems, show_answers=False):
    count_multiply = 0
    count_division = 0
    count_total = 0
    operator_check_length = float(-2)*len(problems)
    digits_check_list = []
    list_of_more_than_4_digits = []
    list_of_less_than_4_digits = []
    number_of_dashes_list = []    
    row1 = []
    row2 = []
    row3 = []
    row4 = []
    if len(problems) > 5:
        return('Error: Too many problems.')
    else:
        for individual_problem in problems:
            multiply_check = individual_problem.find('*')
            division_check = individual_problem.find('/')
            count_multiply += multiply_check
            count_division += division_check
            count_total = count_multiply + count_division
        if count_total != operator_check_length:
            return("Error: Operator must be '+' or '-'.")
        else:
            for individual_problem in problems:
                individual_problem_space_removed = individual_problem.replace(' ', '0')
                if individual_problem_space_removed.find('+') == -1:
                    individual_problem_digits_check = individual_problem_space_removed.replace('-','0') 
                    digits_check_list.append(individual_problem_digits_check)
                elif individual_problem_space_removed.find('-') == -1:
                    individual_problem_digits_check = individual_problem_space_removed.replace('+','0')
                    digits_check_list.append(individual_problem_digits_check)
            converted_to_digits = ''.join(digits_check_list)
            if converted_to_digits.isdigit():
                for problem in problems:
                    split_list = problem.split()
                    for individual_components in split_list:
                        if individual_components == '+' or individual_components == '-':
                            pass
                        elif individual_components.isdigit:
                            if int(individual_components) <10000:
                                list_of_less_than_4_digits.append(individual_components)
                            else:
                                list_of_more_than_4_digits.append(individual_components)
                if list_of_more_than_4_digits == []:
                    for problem in problems:
                        sub_everything = problem.split()
                        row1.append(' '*(max(len(sub_everything[0]),len(sub_everything[2]))+2-len(sub_everything[0]))+sub_everything[0]+' '*4)
                    for problem in problems:
                        sub_everything2 = problem.split()
                        row2.append(sub_everything2[1]+' '*((max(len(sub_everything2[0]),len(sub_everything2[2]))+1)-len(sub_everything2[2]))+sub_everything2[2]+' '*4)
                    for problem in problems:
                        sub_everything3 = problem.split()
                        row3.append('-'*(max(len(sub_everything3[0]),len(sub_everything3[2]))+2)+' '*4)
                    for problem in problems:
                        sub_everything4 = problem.split()
                        row4.append(' '*(max(len(sub_everything4[0]),len(sub_everything4[2]))+2-int(len(str(int(sub_everything4[0])+(int(sub_everything4[1]+'1')*int(sub_everything4[2]))))))+str(int(sub_everything4[0])+(int(sub_everything4[1]+'1')*int(sub_everything4[2])))+' '*4)        
                                              
                    
                    if show_answers is True:
                        return('\n'.join([''.join(row1).rstrip(), ''.join(row2).rstrip(), ''.join(row3).rstrip(),''.join(row4)]))
                    else:
                        return('\n'.join([''.join(row1).rstrip(), ''.join(row2).rstrip(), ''.join(row3).rstrip()]))
                  
                else:
                    return('Error: Numbers cannot be more than four digits.')                               
            else:
                return('Error: Numbers must only contain digits.')
    return problems

if you open the browser console you should see a more detailed output of the tests

Click here for more infos on how to read an AssertionError

The assertion error and diff gives you a lot of information to track down a problem. For example:

AssertionError: 'Year' != 'Years'
- Year
+ Years
?     +

Your output comes first, and the output that the test expects is second.

AssertionError: ‘Year’ != ‘Years’

Your output: Year does not equal what’s expected: Years

This is called a diff, and it shows you the differences between two files or blocks of code:

- Year
+ Years
?     +

- Dash indicates the incorrect output
+ Plus shows what it should be
? The Question mark line indicates the place of the character that’s different between the two lines. Here a + is placed under the missing s .

Here’s another example:

E       AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
E       assert '  3801      123    \n   - 2     + 49    \n------    -----    \n' == '  3801      123\n-    2    +  49\n------    -----'
E         -   3801      123
E         +   3801      123    
E         ?                ++++
E         - -    2    +  49
E         +    - 2     + 49    
E         - ------    -----
E         + ------    -----    
E         ?                +++++

The first line is long, and it helps to view it as 2 lines in fixed width characters, so you can compare it character by character:

'  3801      123    \n   - 2     + 49    \n------    -----    \n'
'  3801      123\n-    2    +  49\n------    -----'

Again, your output is first and the expected output is second. Here it’s easy to see extra spaces or \n characters.

E         -   3801      123
E         +   3801      123    
E         ?                ++++

Here the ? line indicates 4 extra spaces at the end of a line using four + symbols. Spaces are a little difficult to see this way, so it’s useful to use both formats together.

I hope this helps interpret your error!

for example let’s take a look at this:

AssertionError: '    3      988\n+ 855    +  40\n-----    -----\n  858     1028    ' 
             != '    3      988\n+ 855    +  40\n-----    -----\n  858     1028'

the first line is the actual (the output of your code), the second is the expected. It looks like you are missing some spaces at the end

Ohhh thank you so much i forgot to apply the rstrip() method to the last row which is why it had extra spaces.
Thank you so much!! It passed now