Tell us what’s happening:
I don’t know what’s wrong with the code
The code
def arithmetic_arranger(problems, showResult = False):
one = ''
two = ''
lines = ''
third = ''
for i in problems:
if len(problems) > 5:
return "Error: Too many problems."
component = i.split(' ')
first = component[0]
op = component[1]
second = component[2]
if not first.isnumeric() or not second.isnumeric():
return "Error: Numbers must only contain digits."
if len(first) > 4 or len(second) > 4:
return "Error: Numbers cannot be more than four digits."
if op == "+" or op == "-":
if op == "+":
sum = str(int(first) + int(second))
if op == "-":
sum = str(int(first) - int(second))
else:
return "Error: Operator must be '+' or '-'."
length = max(len(first), len(second)) + 2
top = first.rjust(length)
bottom = op + second.rjust(length - 1)
res = sum.rjust(length)
line = ''
for j in range(length):
line += '-'
if i != problems[-1]:
one += top + ' '
two += bottom + ' '
lines += line + ' '
third += res + ' '
else:
one += top
two += bottom
lines += line
third += res
if showResult:
arranged_problems = one + '\n' + two + '\n' + lines + '\n' + third
else:
arranged_problems = one + '\n' + two + '\n' + lines + '\n'
return arranged_problems
Log:
32 3801 45 123
+ 698 - 2 + 43 + 49
----- ------ ---- -----
F.....
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/FatalFunctionalKeyboard/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: ' [36 chars] - 2 + 43 + 49\n----- ------ ---- -----\n' != ' [36 chars] - 2 + 43 + 49\n----- ------ ---- -----'
3 3801 45 123
+ 855 - 2 + 43 + 49
- ----- ------ ---- -----
? -
+ ----- ------ ---- ----- : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]
----------------------------------------------------------------------
Ran 6 tests in 0.002s
FAILED (failures=1)
Challenge: Arithmetic Formatter
Link to the challenge: