Tell us what’s happening:
I’m nearly done with the arithmetic arranger challenge. When I set solve to False it passes all tests, but when I set it to True, it fails one, and I can’t see why because the output looks fine. Anyone who knows how to fix this?
Your code so far
def arithmetic_arranger(problems, solve=True):
if len(problems) > 5:
return("Error: Too many problems.")
x = []
N1 = 0
N2 = 0
OP = ""
sum_output = ""
first = ""
second = ""
lines = ""
sumx = 0
sumxstr = ""
for i in problems:
x = i.split()
try:
N1 = int(x[0])
N2 = int(x[2])
except:
return("Error: Numbers must only contain digits.")
OP = x[1]
if OP != "+" and OP != "-":
return("Error: Operator must be '+' or '-'.")
if len(str(N1)) > 4 or len(str(N2)) > 4:
return("Error: Numbers cannot be more than four digits.")
if OP == "+":
sumx = N1 + N2
if OP == "-":
sumx = N1 - N2
max_len = max(str(N1), str(N2), key=len)
min_len = min(str(N1), str(N2), key=len)
ar_max_len = int(len(max_len)) + 2
ar_min_len = int(len(min_len))
N1_aligned = str(N1).rjust(ar_max_len)
N2_aligned = str(N2)
sumx_aligned = str(sumx).rjust(ar_max_len)
if len(str(N2)) < len(str(N1)):
N2_aligned = str(N2).rjust(int(len(max_len)))
line = '-' * ar_max_len
if i != problems[-1]:
first += N1_aligned + ' '
second += OP + ' ' + N2_aligned + ' '
lines += line + ' '
sumxstr += sumx_aligned + ' '
else:
first += N1_aligned
second += OP + ' ' + N2_aligned
lines += line
sumxstr += sumx_aligned
if solve == True:
sum_output = first + "\n" + second + "\n" + lines + "\n" + sumxstr
else:
sum_output = first + "\n" + second + "\n" + lines
return sum_output
Output
32 3801 45 123
+ 698 - 2 + 43 + 49
----- ------ ---- -----
730 3799 88 172
F.....
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-1/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: ' [68 chars]- ------ ---- -----\n 858 3799 88 172' != ' [68 chars]- ------ ---- -----'
3 3801 45 123
+ 855 - 2 + 43 + 49
- ----- ------ ---- -----
? -
+ ----- ------ ---- ------ 858 3799 88 172 : 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)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0
Challenge: Arithmetic Formatter
Link to the challenge: