Im trying to make the arithmetic arranger and here is my code:
def arithmetic_arrenger(problems, bool= False):
#my approach consists in making one string for every line and then adding teh up at the end.
#This way, sup its the top line, inf, the next line, das the dashes line, and sol its optional in the case of entering True as the second parameter
sup = '' ; sol= '' ; das= '' ; inf= ''
#checks if the number of problems its within the function limit
if len(problems) > 5:
return 'Too many problems'
for problem in problems:
(a,b,c) = problem.split()
#checks the operator
if b=='+' or b=='-':
try:#checks the number its only made of digits
A = int(a)
C = int(c)
except:
return 'numbers must only contain digits'
if len(a) > 4 or len(c) > 4:#checks the length of the digits
return 'Numbers cant have more than 4 digits'
if len(a)>len(c):
dashes = len(a) + 2
sup = sup + 2*' ' + str(a) + 4*' '
inf = inf + b +(len(a)-len(c)+1)*' ' + str(c) + 4*' '
else:
dashes = len(c) + 2
inf = inf + b + ' ' + str(c) + 4*' '
sup = sup + (len(c)-len(a) + 2)*' ' + str(a) + 4*' '
das = das + dashes*'_' + 4* ' '
if bool:
if b == '+':
s = str(A + C)
sol = sol + (dashes-len(s))*' ' + s + 4*' '
else:
s = str(A - C)
sol = sol + (dashes - len(s)) * ' ' + s + 4 * ' '
else:
return 'Operator must be + or -'
if bool:
return sup.rstrip() + '\n' + inf.rstrip() + '\n' + das.rstrip() + '\n' + sol.rstrip()
else:
return sup.rstrip() + '\n' + inf.rstrip() + '\n' + das.rstrip()
when i run it on pycharm it goes well, but on the test it fails twice:
FAIL: test_arrangement (test_module.UnitTests)
Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter-2/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: “Error: Operator must be ‘+’ or ‘-’.” != ’ 3 3801 45 123\n+ 855 [56 chars]----’
- Error: Operator must be ‘+’ or ‘-’.
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-2/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 ofTrue
.’)
AssertionError: “Error: Operator must be ‘+’ or ‘-’.” != ’ 32 1 45 123\n- 698 [90 chars] 172’
- Error: Operator must be ‘+’ or ‘-’.
- 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 ofTrue
.
i need help to find where is my error.