Arithmetic Formatter failing with one error

Tell us what’s happening:
Hi All,
Please help me resolve errors in my code.

def arithmetic_arranger(problems, option=False):
    if len(problems) > 5:
        return "Error: Too many problems."
    line_1 = line_2 = line_3 = line_4 = ' '
    for i, problem in enumerate(problems):
        # operators = {'+', '-'}

        n1, operator, n2 = problem.split()
        number_1, number_2 = len(n1), len(n2)
        max_space = max(number_1, number_2)
        if operator not in ['+', '-']:
            return "Error: Operator must be '+' or '-'."
        if not (n1.isdigit()) or n2.isdigit():
            return "Error: Numbers must only contain digits."
        if number_1 > 4 and number_2 > 4:
            return "Error: Numbers cannot be more than four digits."

        result = int(n1) + int(n2) if operator == '+' else int(n1) - int(n2)

        line_1 = line_1 + f"{n1:>2}"
        line_2 = line_2 + operator + f"{n2:>1}"
        line_3 = line_3 + '' + f"{'-':>1}"
        line_4 = line_4 + str(result).rjust(max_space + 2)

        if i < len(problems) - 1:
            line_1 += ' '
            line_2 += ' '
            line_3 += ' '
            line_4 += ' '

        if option:
            return f"arranged_problems: {line_1}, \n{line_2}, \n{line_3}, \n{line_4}"
        else:
            return f"arranged_problems: {line_1}, \n{line_2}, \n{line_3}"
        return arranged_problems

Error:

ERROR: test_only_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 32, in test_only_digits
    actual = arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])
  File "/home/runner/boilerplate-arithmetic-formatter/arithmetic_arranger.py", line 18, in arithmetic_arranger
    result = int(n1) + int(n2) if operator == '+' else int(n1) - int(n2)
ValueError: invalid literal for int() with base 10: '3g5'

Your code so far

Your browser information:

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

Challenge: Arithmetic Formatter

Link to the challenge:

There are several problems, but the unit tests are telling you where to look. The test error you posted

is showing you that during the check for digits only, the 3g5 got through and caused the program to fail when you are trying to add/subtract. The other tests that failed when I ran your code all said

AssertionError: 'Error: Numbers must only contain digits.' != '...'

which indicates again that your check for digits is not working. In your code

the if ... is not doing what you want. You want to return the error if both n1 and n2 are not digits. Likewise here

you want to return the error if either number_1 or number_2 is too big.

If you fix these, the unit tests will be more helpful in fixing remaining issues.

Good luck.

Hi @jeremy.a.gray, thanks for the reply. I tried with above solutions, but my code is still not passing test case. It’s throwing ValueError. Please help.

def arithmetic_arranger(problems, option=False):
    if len(problems) > 5:
        return "Error: Too many problems."
    line_1 = line_2 = line_3 = line_4 = ' '
    for i, problem in enumerate(problems):
        # operators = {'+', '-'}

        n1, operator, n2 = problem.split()
        number_1, number_2 = len(n1), len(n2)
        max_space = max(number_1, number_2)
        if operator not in ['+', '-']:
            return "Error: Operator must be '+' or '-'."
        if not (n1.isdigit()) and not (n2.isdigit()):
            return "Error: Numbers must only contain digits."
        if number_1 > 4 or number_2 > 4:
            return "Error: Numbers cannot be more than four digits."

        result = int(n1) + int(n2) if operator == '+' else int(n1) - int(n2)
        

        line_1 = line_1 + f"{n1:>2}"
        line_2 = line_2 + operator + f"{n2:>1}"
        line_3 = line_3 + ''.rjust(max_space + 1, '-')
        line_4 = line_4 + str(result).rjust(max_space)

        if i < len(problems) - 1:
            line_1 += ' '
            line_2 += ' '
            line_3 += ' '
            line_4 += ' '

        if option:
            arranged_problems = line_1 + '\n' + line_2 + '\n' + line_3 + '\n' + line_4
        else:
            arranged_problems = line_1 + '\n' + line_2 + '\n' + line_3
        return arranged_problems

Error:

=======
ERROR: test_only_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-1/test_module.py", line 32, in test_only_digits
    actual = arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])
  File "/home/runner/boilerplate-arithmetic-formatter-1/arithmetic_arranger.py", line 18, in arithmetic_arranger
    result = int(n1) + int(n2) if operator == '+' else int(n1) - int(n2)
ValueError: invalid literal for int() with base 10: '3g5'

======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-1/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: '  3 \n +855 \n ---- ' != '    3      3801      45      123\n+ 855    [53 chars]----'
-   3 
-  +855 
-  ---- +     3      3801      45      123
 python main.py
 32 
 +698 
 ---- 
F.EF..
======================================================================
ERROR: test_only_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-1/test_module.py", line 32, in test_only_digits
    actual = arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])
  File "/home/runner/boilerplate-arithmetic-formatter-1/arithmetic_arranger.py", line 18, in arithmetic_arranger
    result = int(n1) + int(n2) if operator == '+' else int(n1) - int(n2)
ValueError: invalid literal for int() with base 10: '3g5'

======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-1/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: '  3 \n +855 \n ---- ' != '    3      3801      45      123\n+ 855    [53 chars]----'
-   3 
-  +855 
-  ---- +     3      3801      45      123
 python main.py
 32 
 +698 
 ---- 
F.EF..
======================================================================
ERROR: test_only_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-1/test_module.py", line 32, in test_only_digits
    actual = arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])
  File "/home/runner/boilerplate-arithmetic-formatter-1/arithmetic_arranger.py", line 18, in arithmetic_arranger
    result = int(n1) + int(n2) if operator == '+' else int(n1) - int(n2)
ValueError: invalid literal for int() with base 10: '3g5'

======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-1/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: '  3 \n +855 \n ---- ' != '    3      3801      45      123\n+ 855    [53 chars]----'
-   3 
-  +855 
-  ---- +     3      3801      45      123
+ + 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/boilerplate-arithmetic-formatter-1/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: ' 32 \n -698 \n ---- \n -666 ' != '   32         1      45      123\n- 698   [88 chars] 172'
-  32 
-  -698 
-  ---- 
-  -666 +    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 arithemetic problems and a second argument of `True`.

----------------------------------------------------------------------
Ran 6 tests in 0.003s

FAILED (failures=2, errors=1)
 

This is the same error as before with the same cause; this conditional is not working correctly:

As written, the conditional is only true when n1 is not all digits and n2 is not all digits. You need it to be true if either n1 or n2 is not all digits. It is also logically equivalent to say n1 and n2 are not both all digits. Or, not (P and Q) is equivalent to (not P or not Q).

The other errors are because your code is only returning one problem out of the four it should, which means your loop is stopping after one loop instead of executing four times. Usually that means a return statement or some other loop terminating statement is at the wrong level of indentation. Once you are returning four problems, then you may need to adjust the spacing to pass the tests.