Hi I tested my code on the Arithmetic Arranger project and it seems fine but it does not pass the test, can you help me find what’s wrong?
thank you in advance
problems=["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]
def arithmetic_arranger(problems, flag=False):
line1 = line2 = line3 = line4 = ""
if len(problems)>5:
return print("Error: Too many problems.")
for problem in problems:
words = problem.split()
num1 = words[0]
op = words[1]
num2 = words[2]
bar_len = len(max(num1,num2))+2
if len(op)!=1 or op not in ['+','-']:
return print("Error: Operator must be '+' or '-'.")
if not (num1.isdigit() and num2.isdigit()):
return print("Error: Numbers must only contain digits.")
if not (len(num1)<=4 and len(num2)<=4):
return print("Error: Numbers cannot be more than four digits.")
if op == '+':
ris = int(num1)+int(num2)
elif op == '-':
ris = int(num1)-int(num2)
line1 += " "*(bar_len-len(str(num1))) + str(num1) + " "*4
line2 += op + " "*(bar_len-len(str(num2))-1) + str(num2) + " "*4
line3 += "-"*(bar_len) + " "*4
line4 += " "*(bar_len-len(str(ris))) + str(ris) + " "*4
if flag == False:
return print(line1[:-4] + "\n" + line2[:-4] + "\n" + line3[:-4])
else:
return print(line1[:-4] + "\n" + line2[:-4] + "\n" + line3[:-4] + "\n" + line4[:-4])
arithmetic_arranger(problems,True)