Arithmetic Formatter error help

Tell us what’s happening:
so i began with this python for everybody course with abosolutely no knowledge about python. i started with this project some time ago but i keep getting this error and couldnt see what is wrong in this code.

Your code so far

        def arithmetic_arranger(problems, con=False):
    if len(problems) > 5:
      return 'Error: Too many problems.'
    for i in problems:
      l = (i.split(' '))
      num1 = l[0]
      op = l[1]
      num2 = l[2]
      sums=num1+num2
      if len(num1) > 4 or len(num2) > 4:
        return 'Error: Numbers cannot be more than four digits.'
      if not num1.isnumeric() or not num2.isnumeric():
        return 'Error: Numbers must only contain digits.'
      if  op == '+' or op == '-':
        length = max(len(num1), len(num2)) + 2
        while (len(num1)<length):
          num1=' '+num1
        while (len(num2)<(length-1)):
          num2=' '+num2
        if not con:
          arranged_problems = num1 + '\n' +op+ num2+'\n'+('-'*length)+'\n'+ sums + '\n'+('-'*length)
          return arranged_problems
        else: 
          arranged_problems = num1 + '\n' +op+ num2+ '\n'+('-'*length)
          return arranged_problems
    
      else: 
        return "Error: Operator must be '+' or '-'."
       

errors encountered

/ Starting Repl...   32
+ 698
-----
32698
-----
F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/yrwt8p0z24s/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-----\n3855\n-----' != '    3      3801      45      123\n+ 855    -  [50 chars]----'
+     3      3801      45      123
+ + 855    -    2    + 43    +  49
+ -----    ------    ----    ------     3
- + 855
- -----
- 3855
- ----- : 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/yrwt8p0z24s/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-----' != '   32         1      45      123\n- 698    - 3[84 chars] 172'
-    32
- - 698
- -----+    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.002s

FAILED (failures=2)

Connection closed abruptly

Your browser information:

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

Challenge: Arithmetic Formatter

Link to the challenge:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Welcome to the forums. When I run the tests locally, I see:

ERROR: test_incorrect_operator (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/gray/src/work/fcc-sc-arithmetic-arranger/test_module.py", line 23, in test_incorrect_operator
    actual = arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"])
  File "/home/gray/src/work/fcc-sc-arithmetic-arranger/arithmetic_arranger.py", line 71, in arithmetic_arranger
    while (len(num1)<length):
UnboundLocalError: local variable 'length' referenced before assignment

referring to this bit of code. If the operator doesn’t match ‘+/-’, then length isn’t set and the operator is testing ‘/’.

This error

FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
    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-----' != '   32         1      45      123\n- 698    - 3[84 chars] 172'
-    32
- - 698
- -----+    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`.

shows (your output is the ‘-’ lines, the ‘+’ lines are the expected ones) that you are only printing the first problem, and no solution. The other error in test_arrangement also appears due to only printing one problem. Both these problems are because you are returning in the conditional at the end on the first iteration, not after all the problems are printed.

Good luck.

2 Likes