Tell us what’s happening:
I tried all the tests by hand and my code works as expected. But when I “run the tests” I cannot pass them except the error ones. The output looks the same so what could be the problem here?
Your code so far
def arithmetic_arranger(problems, show_answers=False):
firstRow = ""
secondRow = ""
thirdRow = ""
lines = ""
row = ""
for problem in problems:
if len(problems) > 5:
return "Error: Too many problems."
ops = problem.split()
if len(ops[0]) > 4 or len(ops[2]) > 4:
return "Error: Numbers cannot be more than four digits."
if ops[1] == "*" or ops[1] == "/":
return "Error: Operator must be '+' or '-'."
if not ops[0].isdigit() or not ops[2].isdigit():
return "Error: Numbers must only contain digits."
if len(ops[0]) > len(ops[2]):
lines = "-" * (len(ops[0]) + 2)
befSpace1 = " " * 2
befSpace2 = " " * (len(ops[0])-len(ops[2]) +1)
else:
lines = "-" * (len(ops[2]) + 2)
befSpace1 = " " * (len(ops[2])-len(ops[0]) +2)
befSpace2 = " "
firstRow += befSpace1 + ops[0] + " "
secondRow += ops[1] + befSpace2 + ops[2] + " "
thirdRow += lines + " "
output = firstRow + "\n" + secondRow + "\n" + thirdRow
if show_answers:
if ops[1] == "+":
answer = int(ops[0]) + int(ops[2])
else:
answer = int(ops[0]) - int(ops[2])
row += " " * (len(lines) - len(str(answer))) + str(answer) + " "
output += "\n" + row
return output
print(f'\n{arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)}')
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:132.0) Gecko/20100101 Firefox/132.0
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project