Arithmetic formatter: arrangement and solution tests failed

Tell us what’s happening:
I think the result I return in the function is formatted differently than expected in the tests. I checked the traceback for both tests but I can’t find exactly how to see the errors (I am a bit new to Python). I think it may be something very silly but it has been hours without finding out a solution.
I appreciate each answer in advance!

Your code so far

def arithmetic_arranger(problems, show = False):
    # Lenght of list error:
    if len(problems) > 5:
        return "Error: Too many problems."
    else:
        # Assign variable to each line
        first_line = ""
        second_line = ""
        dash_line = ""
        answer_line = ""
        # Iterate through the operation on the list:
        for i in problems:
            # Split the operation using whitespaces as separators and saving each value
            # on a different variable 
            items = i.split()
            operand_1 = items[0]
            operand_2 = items[-1]
            symbol = items[1]
            # Expected max width in each operation:
            width = max(len(operand_1), len(operand_2)) + 2
            
            # Digit only error:
            if not operand_1.isdigit() or not operand_2.isdigit():
                return "Error: Numbers must only contain digits."
            # Symbol specific error:
            else:
                if symbol == "+":
                    answer = int(operand_1) + int(operand_2)
                elif symbol == "-":
                    answer = int(operand_1) - int(operand_2)
                else:
                    return "Error: Operator must be '+' or '-'."
                
                # Max length of digits error
                if len(operand_1) > 4 or len(operand_2) > 4:
                    return "Error: Numbers cannot be more than four digits."
                
                # Add to each line its value in order, adjusting width to align them to the right
                first_line += str(operand_1).rjust(width)
                second_line += symbol +str(operand_2).rjust(width - 1)
                dash_line += "-" * width
                answer_line += str(answer).rjust(width)
                
                # In case of more than one operation, add 4 whitespaces before adding
                # the next set of numbers
                if len(problems) > 1:
                    first_line += "    "
                    second_line += "    "
                    dash_line += "    "
                    answer_line += "    "
                
                # Write in a new variable all the lines, defined with newlines to match correctly
                # If second value of function set to true, it displays the answers
                if show == True:
                    problem_arranged = (first_line + "\n" + second_line + "\n" + dash_line + "\n" + answer_line)
                else:
                    problem_arranged = (first_line + "\n" + second_line + "\n" + dash_line)
        
        
        return problem_arranged

The traceback of the two tests:

======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/SnarlingHumbleWebpage/test_module.py", line 10, in test_arrangement
    self.assertEqual(actual, expected, 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]')
AssertionError: '    [23 chars]  123    \n+ 855    -    2    + 43    +  49   [35 chars]    ' != '    [23 chars]  123\n+ 855    -    2    + 43    +  49\n-----[23 chars]----'
-     3      3801      45      123    
?                                 ----
+     3      3801      45      123
- + 855    -    2    + 43    +  49    
?                                 ----
+ + 855    -    2    + 43    +  49
- -----    ------    ----    -----    ?                                 ----
+ -----    ------    ----    ----- : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]

======================================================================
FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/SnarlingHumbleWebpage/test_module.py", line 39, in test_solutions
    self.assertEqual(actual, expected, 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithemetic problems and a second argument of `True`.')
AssertionError: '   3[23 chars]  123    \n- 698    - 3801    + 43    +  49   [73 chars]    ' != '   3[23 chars]  123\n- 698    - 3801    + 43    +  49\n-----[57 chars] 172'
-    32         1      45      123    
?                                 ----
+    32         1      45      123
- - 698    - 3801    + 43    +  49    
?                                 ----
+ - 698    - 3801    + 43    +  49
- -----    ------    ----    -----    
?                                 ----
+ -----    ------    ----    -----
-  -666     -3800      88      172    ?                                 ----
+  -666     -3800      88      172 : Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithemetic problems and a second argument of `True`.

----------------------------------------------------------------------

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36.

Challenge: Arithmetic Formatter

Link to the challenge:

Hello! Welcome to the community :partying_face:!

The problem is that you have trailing spaces on each line, hence the error:

@skaparate
I’ll be happy to know how did you printed this type of output…
Is it debug mode on repl.it or it is, any other site ?

that seems to be http://pythontutor.com/

but if you look in the test output in repl.it it says the same thing, the line starting with ? indicates the differences between what should be and what instead is, and it has 4 dashes to the right of the numbers, so there are extra characters at the end of the line

1 Like

Thanks for welcoming me and for giving me that nice feedback! As I supposed the error was that simple that I could not detect it by myself. The code is already solved and passes all the tests. With a r.strip() added to each variable of the lines obtained I removed the trailing spaces at its right.
Thank you for your help!

1 Like

Now that I see where is the problem I can check properly in the traceback what the eror was, but with my first time looking this type of traceback I couldn’t figure out before that it was showing trailing spaces. Thank you for your explanation and insight!

1 Like

Sorry for not pointing that out, my bad :sweat_smile:! Just to confirm what @ieahleen has already said, it’s http://www.pythontutor.com/.

2 Likes

why don’t you open a topic for your own question?

note that pythontutor needs a function call to run, not just a function definition