Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

I tried all the tests by hand and my code works as expected. But when I “run the tests” I cannot pass them except the error ones. The output looks the same so what could be the problem here?

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    firstRow = ""
    secondRow = ""
    thirdRow = ""
    lines = ""
    row = ""
    for problem in problems:
        if len(problems) > 5:
            return "Error: Too many problems."
        ops = problem.split()
        if len(ops[0]) > 4 or len(ops[2]) > 4:
            return "Error: Numbers cannot be more than four digits."
        if ops[1] == "*" or ops[1] == "/":
            return "Error: Operator must be '+' or '-'."
        if not ops[0].isdigit() or not ops[2].isdigit():
            return "Error: Numbers must only contain digits."
        if len(ops[0]) > len(ops[2]):
            lines = "-" * (len(ops[0]) + 2)
            befSpace1 = " " * 2
            befSpace2 = " " * (len(ops[0])-len(ops[2]) +1)
        else:
            lines = "-" * (len(ops[2]) + 2)
            befSpace1 = " " * (len(ops[2])-len(ops[0]) +2)
            befSpace2 = " "
        firstRow += befSpace1 + ops[0] + "    "
        secondRow += ops[1] + befSpace2 + ops[2] + "    "
        thirdRow += lines + "    "
        output = firstRow + "\n" + secondRow + "\n" + thirdRow
        if show_answers:
            if ops[1] == "+":
                answer = int(ops[0]) + int(ops[2])
            else:
                answer = int(ops[0]) - int(ops[2])
            row += " " * (len(lines) - len(str(answer))) + str(answer) + "    "
            output += "\n" + row

    return output

print(f'\n{arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)}')

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:132.0) Gecko/20100101 Firefox/132.0

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

1 Like

Let’s take a look at the browser console where there is a more detailed output:

AssertionError: '   3[32 chars]  988    \n- 698    - 3801    + 43    +  49   [100 chars]    ' 
             != '   3[32 chars]  988\n- 698    - 3801    + 43    +  49    +  [84 chars]1028'

the first is from your code, the second is the expected. You have an issue with spaces

-    32         1      45      123      988    
?                                          ----
+    32         1      45      123      988
- - 698    - 3801    + 43    +  49    +  40    
?                                          ----
+ - 698    - 3801    + 43    +  49    +  40
- -----    ------    ----    -----    -----    
?                                          ----
+ -----    ------    ----    -----    -----
-  -666     -3800      88      172     1028    
?                                          ----
+  -666     -3800      88      172     1028

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!

2 Likes

I see! What a small difference… Thank you so much for the quick reply!