Tell us what’s happening:
So my code passes all the tests, but is not getting any answer right (for some reason). Can someone explain why
Your code so far
def arithmetic_arranger(problems, show_answers=False):
first_line = []
second_line = []
dashes = []
results = []
for prob in problems:
num1, operator, num2 = prob.split()
if len(problems) > 5:
return print('Error: Too many problems.')
elif "x" in prob or "X" in prob or "/" in prob or "%" in prob:
return print("Error: Operator must be '+' or '-'.")
elif num1.isdigit() == False or num2.isdigit() == False:
return print('Error: Numbers must only contain digits.')
elif max(len(str(num1)), len(str(num2))) > 4:
return print('Error: Numbers cannot be more than four digits.')
else:
spacelines = "-" * (max(len(str(num1)), len(str(num2))) + 2)
spacenum1 = " " * (len(spacelines) - len(num1))
spacenum2 = " " * (max(len(str(num1)), len(str(num2))) - len(num2))
spaceres = " " * (len(spacelines) - (max(len(str(num1)), len(str(num2)), len(str(int(num1) + int(num2))))))
spaceresn = " " * (len(spaceres)-1)
num1 = int(num1)
num2 = int(num2)
first_line.append(f"{spacenum1}{num1}")
second_line.append(f"{operator} {spacenum2}{num2}")
dashes.append(spacelines)
if show_answers:
result = num1 + num2 if operator == '+' else num1 - num2
if result > 0:
results.append(f"{spaceres}{result}")
else:
results.append(f"{spaceresn}{result}")
print(" ".join(first_line))
print(" ".join(second_line))
print(" ".join(dashes))
if show_answers:
print(" ".join(results))
arithmetic_arranger(["3801 - 2", "123 + 49"])
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project