Hi everyone, I tried my best at it, looked up at other threads with the same topic. Seems like I still can’t get it quite right:
def arithmetic_arranger(problems, optionalPara = False):
n1r_f = ""
n2r_f = ""
line_f = ""
solution_f = ""
if len(problems) > 5:
return("Error: Too many problems.")
else:
for problem in problems:
i = problem.split()
n1 = i[0]
op = i[1]
n2 = i[2]
if op not in ['+','-']:
return("Error: Operator must be '+' or '-'.")
else:
if not n1.isnumeric() or not n2.isnumeric():
return("Error: Numbers must only contain digits.")
else:
if len(n1)>4 or len(n2)>4:
return("Error: Numbers cannot be more than four digits.")
else:
if(op == '+'):
solution = str(int(n1) + int(n2))
else:
solution = str(int(n1) - int(n2))
length = max(len(n1),len(n2)) + 2
n1r = str(n1.rjust(length))
n2r = str(op + n2.rjust(length-1))
line = str('-' * length)
solutionLine = str(solution).rjust(length)
if problem != problems[-1]:
n1r_f += n1r + ' '
n2r_f += n2r + ' '
line_f += line + ' '
solution_f += solutionLine + ' '
else:
n1r_f +=n1r
n2r_f +=n2r
line_f +=line
solution_f +=solutionLine
if optionalPara:
arranged_problems = n1r_f + '\n' + n2r_f + '\n' + line_f + '\n'
else:
solution_f = solution_f.rstrip()
arranged_problems = n1r_f + '\n' + n2r_f + '\n' + line_f + '\n' + solution_f
return arranged_problems
The output is the following:
python main.py
32
+ 698
-----
730
F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-4/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----- \n 858' != ' 3 3801 45 123\n+ 855 - 2 [46 chars]----'
+ 3 3801 45 123
+ + 855 - 2 + 43 + 49
+ ----- ------ ---- ------ 3
- + 855
- -----
- 858 : 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-4/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: ' 32 \n- 698 \n----- \n' != ' 32 1 45 123\n- 698 - 3801 [80 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 arithmetic problems and a second argument of `True`.
----------------------------------------------------------------------
Ran 6 tests in 0.002s
FAILED (failures=2)
Anyone got a suggestion how to solve it?
PS: Sorry if I formatted sth. wrong, first post