Tell us what’s happening:
always failure on formatting output… can you help me to see what’s wrong with my code
Your code so far
def arithmetic_arranger(problems, statprint=False):
# check problem list
first = ''
second = ''
sumx = ''
lines = ''
# maximal problems is 5
if len(problems) >= 6:
return 'Error: Too many problems.'
# split problem to separate components
for i in problems:
a = i.split()
firsts = a[0]
seconds = a[2]
operands = a[1]
# check the length of the number, max 4 digits
if (len(firsts) > 4 or len(seconds) > 4):
return "Error: Numbers cannot be more than four digits."
# check the input as valid digits
if not firsts.isnumeric() or not seconds.isnumeric():
return "Error: Numbers must only contain digits."
if (operands == '+' or operands == '-'):
if operands == "+":
sums = str(int(firsts) + int(seconds))
else:
sums = str(int(firsts) - int(seconds))
# set length of sum and top, bottom and line values
length = max(len(firsts), len(seconds)) + 2
top = str(firsts).rjust(length)
bottom = operands + str(seconds).rjust(length - 1)
line = ''
res = str(sums).rjust(length)
for s in range(length):
line += '-'
# add to the overall string
first += top + ' '
second += bottom + ' '
lines += line + ' '
sumx += res + ' '
else:
return "Error: Operator must be '+' or '-'."
# strip out spaces to the right of the string
first.rstrip()
second.rstrip()
lines.rstrip()
if statprint:
sumx.rstrip()
arranged_problems = first + '\n' + second + '\n' + lines + '\n' + sumx
else:
arranged_problems = first + '\n' + second + '\n' + lines
return arranged_problems
Here the output
32 3801 45 123
+ 698 - 2 + 43 + 49
----- ------ ---- -----
F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/fcc-arithmetic-arranger/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: ' [23 chars] 123 \n+ 855 - 2 + 43 + 49 [35 chars] ' != ' [23 chars] 123\n+ 855 - 2 + 43 + 49\n-----[23 chars]----'
- 3 3801 45 123
? ----
+ 3 3801 45 123
- + 855 - 2 + 43 + 49
? ----
+ + 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/fcc-arithmetic-arranger/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: ' 3[23 chars] 123 \n- 698 - 3801 + 43 + 49 [73 chars] ' != ' 3[23 chars] 123\n- 698 - 3801 + 43 + 49\n-----[57 chars] 172'
- 32 1 45 123
? ----
+ 32 1 45 123
- - 698 - 3801 + 43 + 49
? ----
+ - 698 - 3801 + 43 + 49
- ----- ------ ---- -----
? ----
+ ----- ------ ---- -----
- -666 -3800 88 172 ? ----
+ -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.012s
FAILED (failures=2)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36
.
Challenge: Arithmetic Formatter
Link to the challenge: