Hi everybody! thank you in advance for reading this and trying to help, just finished the python course so I’m still learning…
just coded the arithmetic arranged function, But I’m getting 2 fails and i don’t know why.
Here’s my code:
import operator
def conv_i(x):
try:
x = int(x)
return True
except:
return False
def arithmetic_arranger(problems, con= False):
arr = ""
signs = {"+":operator.add, "-":operator.sub}
row1 = [*range(len(problems))]
row2 = [*range(len(problems))]
row3 = [*range(len(problems))]
sign = [*range(len(problems))]
if len(problems) > 5:
return print("Error: Too many problems.")
x = 0
for i in problems:
row1[x], sign[x], row2[x] = i.split()
n = i.split()
if len(row1[x]) > 4 or len(row2[x]) > 4:
return print("Error: Numbers cannot be more than four digits.")
elif sign[x] not in signs:
return print("Error: Operator must be '+' or '-'.")
elif conv_i(row1[x]) and conv_i(row2[x]) is False:
return print("Error: Numbers must contain only digits.")
x+= 1
y=0
for i in range(len(problems)):
row3[y]= str(signs[sign[y]](int(row1[y]), int(row2[y])))
y+=1
#Now we need to format row1
for i in range(len(problems)):
diff = max(len(row1[i]), len(row2[i])) + 2
if i == 3:
arr += str(" "*(diff-len(row1[i]))+ row1[i])
continue
arr += str(" "*(diff-len(row1[i]))+ row1[i] + " "*4)
arr+= str("\n")
#format row2
for i in range(len(problems)):
diff = max(len(row1[i]), len(row2[i])) + 1
if i == 3:
arr += str(sign[i]+" "*(diff-len(row2[i]))+ row2[i])
continue
arr += str(sign[i]+" "*(diff-len(row2[i]))+ row2[i] + " "*4)
arr += str("\n")
#format lines
for i in range(len(problems)):
diff = max(len(row1[i]), len(row2[i])) + 2
if i == 3:
arr += str("-"*diff)
continue
arr += str("-"*diff + " "*4)
arr += str("\n")
#format optional results
if con:
for i in range(len(problems)):
diff = max(len(row1[i]), len(row2[i])) + 2
if i == 3:
arr+= str(" "*int(diff-len(row3[i])) + row3[i])
continue
arr+= str(" "*int(diff-len(row3[i])) + row3[i] + (" "*4))
return arithmetic_arranger
and these are the fails:
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/Aritarranger/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: <function arithmetic_arranger at 0x7f20a1627940> != ' 3 3801 45 123\n+ 855 [56 chars]----' : 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/Aritarranger/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: <function arithmetic_arranger at 0x7f20a1627940> != ' 32 1 45 123\n- 698 [90 chars] 172' : Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithemetic problems and a second argument of `True`.
if you run my program, you’ll get the exact same results.
what did i do wrong?