I was working on the Python project. Build an Arithmetic Formatter. Everything seems to be working and even when I tested the values on the run test it seems to not pass. I triple checked all my spacing and yet doesn’t seem to work. Help would be much appreciated.
def arithmetic_arranger(problems, show_answers=False):
if len(problems) > 5:
return 'Error: Too many problems.'
#Adds equations to a list
equations = []
for eq in problems: #iterate equation strings
equation = []
longest_length = 0
num = ''
for i in eq: #iterate through each char in string
if i.isalpha():
return 'Error: Numbers must only contain digits.'
if i == ' ':
if not num == '':
equation.append(num)
#adding longest length
if len(num) > longest_length:
longest_length = len(num)
num = ''
else:
num += i
equation.append(num)
if len(num) > longest_length:
longest_length = len(num)
equation.append(longest_length)
equations.append(equation)
result = ""
for eq in equations:
#Error Checks
if eq[1] == '+' or eq[1] =='-':
pass
else:
return "Error: Operator must be '+' or '-'."
if len(eq[2]) > 4 or len(eq[0]) > 4:
return 'Error: Numbers cannot be more than four digits.'
#Line 1
for eq in equations:
spaces = eq[3] + 2 - len(eq[0])
result += (" " * spaces) + eq[0] + (" " * 4)
#Line 2
result += '\n'
for eq in equations:
result += eq[1]
spaces = eq[3] + 1 - len(eq[2])
result += (" " * spaces) + eq[2] + (" " * 4)
#Line 3
result += '\n'
for eq in equations:
dash = eq[3] + 2
result += ("-" * dash) + (" " * 4)
#Answers
if show_answers:
result += '\n'
for eq in equations:
if eq[1] == '+':
answer = int(eq[0]) + int(eq[2])
else:
answer = int(eq[0]) - int(eq[2])
spaces = eq[3] + 2 - len(str(answer))
result += (" " * spaces) + str(answer) + (" " * 4)
return result
print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], True)}')


