Tell us what’s happening:
The preview shows correct formatting, but the code didn’t pass the formatting tests.
Your code so far
def arithmetic_arranger(problems, show_answers=False):
top = ""
bottom = ""
line = ""
tline = ""
if len(problems) > 5:
return ('Error: Too many problems.')
for problem in problems:
firstNumber = problem.split()[0]
secondNumber = problem.split()[2]
operator = problem.split()[1]
maxlength = max(len(firstNumber), len(secondNumber))
if operator != '+' and operator != '-':
return "Error: Operator must be '+' or '-'."
if not firstNumber.isdigit() or not secondNumber.isdigit():
return 'Error: Numbers must only contain digits.'
if maxlength > 4:
return 'Error: Numbers cannot be more than four digits.'
if operator == '+':
result = str(int(firstNumber) + int(secondNumber))
else:
result = str(int(firstNumber) - int(secondNumber))
top = top + str(firstNumber).rjust(maxlength + 2) + 4 * " "
bottom = bottom + operator + str(secondNumber).rjust(maxlength + 1) + 4 * " "
line = line + '-' * (maxlength + 2) + 4 * " "
if show_answers:
tline = tline + result.rjust(maxlength + 2) + 4 * " "
problems = top + "\n" + bottom + "\n" + line + "\n" + tline
return problems
print(arithmetic_arranger(["3801 - 2", "123 - 49"]))
print(arithmetic_arranger(["1 + 2", "1 - 9380"]))
print(arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]))
print(arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]))
print(arithmetic_arranger(["3 + 855", "988 + 40"], True))
print(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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project