Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

My code works when I test it with problems.
But the console tests keep failing.
I have no idea what to do anymore

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    first_line = ''
    second_line = ''
    third_line = ''
    fourth_line = ''
    if len(problems) > 5:
        print('Error: Too many problems.')
    else: 
        problems = [problem.split() for problem in problems] #list compression to seperate the numbers and operators
        for operand in problems:
            first = operand[0] #assigning numbers and operator of each problem to variables
            operator = operand[1]
            second = operand[2]
            if operator == '+' or operator == '-': #making sure operations are additions and subraction only
                if first.isdigit() and second.isdigit(): #making sure numbers are numericals only
                    if len(first) > 4 or len(second) > 4: #maximum of four digits per number
                        print('Error: Numbers cannot be more than four digits.')
                    else:
                        #main code location
                        width = max(len(first), len(second)) + 2 #number of spaces allocated to each operation
                        result = None #to declare a variable that will store the result of each operation
                        if operator == '+':
                            result = int(first) + int(second)
                        elif operator == '-':
                            result = int(first) - int(second)
                        first_line +=str(first).rjust(width)+'    '#the variables are strings already, no need for the format string
                        second_line +=operator+str(second).rjust(width - 1)+'    '
                        third_line +='-'*width+'    '
                        fourth_line +=str(result).rjust(width)+'    '
                else:
                    print('Error: Numbers must only contain digits.')
            elif operator == '*' or operator == '/':
                print("Error: Operator must be '+' or '-'.")
    if show_answers == True: 
        problems = f'{first_line}\n{second_line}\n{third_line}'
    else:
        problems = f'{first_line}\n{second_line}\n{third_line}\n{fourth_line}'
    return problems

arithmetic_arranger(["3801 - 2", "123 + 49"])

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) 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

if you open the browser console like suggested you can see a more detailed version of the test output.

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!