Arithmetic formatter not working

I don’t know what is wrong with the code. The code must give multiple answers(if there are two problems like arithmetic_arranger(["32 + 698", "5 + 3"]) the output should be two answers ) But It only gives single one.

Here’s 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 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 not firsts.isnumeric() or not seconds.isnumeric():
        return "Error: Numbers must only contain digits."
    elif (len(firsts) > 4 or len(seconds) > 4):
        return "Error: Numbers cannot be more than four digits."
    else:
        pass

    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

The error:

 python main.py
  5    
+ 3    
---    
  8    
FFFFF.
======================================================================
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: '  123    \n+  49    \n-----    \n  172    ' != '    3      3801      45      123\n+ 855    [53 chars]----'
+     3      3801      45      123
+ + 855    -    2    + 43    +  49
+ -----    ------    ----    ------   123    
- +  49    
- -----    
-   172     : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]

======================================================================
FAIL: test_incorrect_operator (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/arithmetic-challenge/test_module.py", line 24, in test_incorrect_operator
    self.assertEqual(actual, expected, '''Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be '+' or '-'."''')
AssertionError: '  123    \n+  49    \n-----    \n  172    ' != "Error: Operator must be '+' or '-'."
+ Error: Operator must be '+' or '-'.-   123    
- +  49    
- -----    
-   172     : Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be '+' or '-'."

======================================================================
FAIL: test_only_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/arithmetic-challenge/test_module.py", line 34, in test_only_digits
    self.assertEqual(actual, expected, 'Expected calling "arithmetic_arranger()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."')
AssertionError: '  123    \n+  49    \n-----    \n  172    ' != 'Error: Numbers must only contain digits.'
+ Error: Numbers must only contain digits.-   123    
- +  49    
- -----    
-   172     : Expected calling "arithmetic_arranger()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."

======================================================================
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: '  123    \n+  49    \n-----    \n  172    ' != '   32         1      45      123\n- 698    [87 chars] 172'
-   123    
- +  49    
- -----    
-   172    +    32         1      45      123
+ - 698    - 3801    + 43    +  49
+ -----    ------    ----    -----
+  -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`.

======================================================================
FAIL: test_too_many_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/arithmetic-challenge/test_module.py", line 29, in test_too_many_digits
    self.assertEqual(actual, expected, 'Expected calling "arithmetic_arranger()" with a problem that has a number over 4 digits long to return "Error: Numbers cannot be more than four digits."')
AssertionError: '  123    \n+  49    \n-----    \n  172    ' != 'Error: Numbers cannot be more than four digits.'
+ Error: Numbers cannot be more than four digits.-   123    
- +  49    
- -----    
-   172     : Expected calling "arithmetic_arranger()" with a problem that has a number over 4 digits long to return "Error: Numbers cannot be more than four digits."

----------------------------------------------------------------------
Ran 6 tests in 0.013s

FAILED (failures=5)

Please let me know what’s wrong. I’ve been solving this problem for almost two weeks

Take a look at the part of code that’s supposed to be responsible for adding problems to the result. Try figure out if that’s working as expected and if not, at what point something is not as it should be.

This line runs through all problems and writes it into 0-D variables. Ofcourse at the end you only have one value in each.

I assume you need to indent the entire following code except the return - so it executed per problem.

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