I don't know what I did wrong in my code (Arithmetic Formatter)

I don’t know how to solve two errors in my code. I can’t think of how to approach it. I am very beginner.

Here is the code

def arithmetic_arranger(problems, answer=True):
    arranged_problems = ""
    if len(problems) > 5:
        return "Error: Too many problems."

    first_line = ""
    second_line = ""
    third_line = ""
    fourth_line = ""
    for problem in problems:
        firsts, operator, seconds = problem.split()
        if not firsts.isdigit() or not seconds.isdigit():
          return "Error: Numbers must only contain digits."
        if operator in ["+", "-"]:
          if operator == "+":
            ans = str(int(firsts) + int(seconds))
          else:
            ans = str(int(firsts) - int(seconds))
        else:
          return "Error: Operator must be '+' or '-'."
        if (len(firsts) > 4 or len(seconds) > 4):
          return "Error: Numbers cannot be more than four digits."


        greater = (len(firsts) if len(firsts) > len(seconds) else len(seconds))
        first_line += (" " * (greater - len(firsts) + 2) + firsts + " " * 4)
        second_line += (operator + " " * (greater - len(seconds) + 1) + seconds + " " * 4)
        third_line += "-" * (greater + 2) + " " * 4
        fourth_line += (" " * (greater - len(ans) + 2) + ans + " " * 4)

    if answer:
      arranged_problems = (first_line + "\n" + second_line + "\n" + third_line + "\n" + fourth_line )
    else:
      arranged_problems = (first_line + "\n" + second_line + "\n" + third_line)        
    return arranged_problems

and Here is the error

python main.py
    3      3801      45      123    
+ 855    -    2    + 43    +  49    
-----    ------    ----    -----    
  858      3799      88      172    
F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/arithmetic-challenge/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   [73 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
- -----    ------    ----    -----    
?                                 -----
+ -----    ------    ----    ------   858      3799      88      172     : 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/arithmetic-challenge/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 arithmetic 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 arithmetic problems and a second argument of `True`.

----------------------------------------------------------------------
Ran 6 tests in 0.004s

FAILED (failures=2)
 

Plus I don’t know how to make a code of my own. I ve been studying in the way of copying codes in books or other people’s codes if I understand the codes I am copying.
I am not sure that’s the right way to study programming.

You came this far by using your method, so it can’t be the complete wrong way of studying. You could adjust this code to see what happens and learn to get a deeper understanding, or look up the commands used.
Or take some courses in the curriculum of freeCodeCamp :wink:
Pytontutor helped me a lot for seeing what exactly happens when I run pieces of code: http://pythontutor.com/

To solve your problem here: Pasting the output of the code here shows trailing spaces, maybe you can start by removing these and see what it looks like then? If it needs more adjustments after that, try and see what the problem (error code) is at that moment.
(- = your output, + = what it is supposed to be, red = wrong, green = correct)

-     3      3801      45      123    
?                                 ----
+     3      3801      45      123
- + 855    -    2    + 43    +  49    
?                                 ----
+ + 855    -    2    + 43    +  49
- -----    ------    ----    -----    
?                                 -----
+ -----    ------    ----    ------   858      3799      88      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

Thanks, I really appreciate you

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.