Tell us what’s happening:
I think my code is working, but when I run the tests it shows me only errors.
I don’t even know why.
Your code so far
def arithmetic_arranger(problems, show_answers=True):
first_row = ""
second_row = ""
space = " "
sub = ""
calc = ""
for problem in problems:
disect = problem.rsplit(" ")
if len(problems) > 5:
print('Error: Too many problems.')
break
elif '-' not in problem and '+' not in problem:
print("Error: Operator must be '+' or '-'.")
break
elif disect[0].isnumeric() == False or disect[2].isnumeric() == False:
print("Error: Numbers must only contain digits.")
break
elif len(disect[0]) > 4 or len(disect[2]) > 4:
print('Error: Numbers cannot be more than four digits.')
break
else:
add = int(disect[0]) + int(disect[2])
subtract = int(disect[0]) - int(disect[2])
if len(problem[0:problem.index(" ")]) < len(problem[problem.index(" ")+2:]):
first_row += (len(problem[problem.index(" ")+1:]) - len(problem[0:problem.index(" ")])) * space + problem[0:problem.index(" ")] + " "
second_row += problem[problem.index(" ")+1:] + " "
sub += len(problem[problem.index(" ")+1:]) * "-" + " "
if disect[1] == "+":
calc += ((len(problem[problem.index(" ")+1:]) - len(str(add))) * " ") + str(add) + " "
else:
calc += ((len(problem[problem.index(" ")+1:]) - len(str(subtract))) * " ") + str(subtract) + " "
elif len(disect[0]) > len(disect[2]):
first_row += " " + problem[0:problem.index(" ")] + " "
second_row += disect[1] + ((len(problem[0:problem.index(" ")]) - len(problem[problem.index(" ")+2:])) +1) * space + problem[problem.index(" ")+2:] + " "
sub += "--" + len(problem[0:problem.index(" ")]) * "-" + " "
if disect[1] == "+":
calc += " " + ((len(problem[0:problem.index(" ")]) - len(str(add))) * " ") + str(add) + " "
else:
calc += " " + ((len(problem[0:problem.index(" ")]) - len(str(subtract))) * " ") + str(subtract) + " "
else:
first_row += problem[0:problem.index(" ")] + " "
second_row += problem[problem.index(" ")+1:] + " "
sub += len(problem[problem.index(" ")+1:]) * "-" + " "
if disect[1] == "+":
calc += str(add) + " "
else:
calc += str(subtract) + " "
if show_answers == True:
print(f'{first_row}\n{second_row}\n{sub}\n{calc}')
else:
print(f'{first_row}\n{second_row}\n{sub}')
arithmetic_arranger(["98 + 35", "3801 - 2", "45 + 43", "123 + 49"])
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project