Tell us what’s happening:
This is my code and my output on the console seems to be right but my tests won’t go through and they are hard to read so I don’t know exactly where to fix my code
Your code so far
def arithmetic_arranger(problems, show_answers=False):
first = ''
second = ''
lines = ''
sumx = ''
if len(problems) > 5:
return('Error: Too many problems')
for problem in problems:
num1 = problem.split(' ')[0]
num2 = problem.split(' ')[2]
operator = problem.split(' ')[1]
if operator not in ['+', '-']:
return "Error: Operator must be '+' or '-'."
if not num1.isdigit() or not num2.isdigit():
return "Error: Numbers must only contain digits."
if(len(num1) >= 5 or (len(num2) >= 5)):
return "Error: Numbers cannot be more than four digits."
sum = ''
if operator == '+':
sum = str(int(num1)) + str(int(num2))
elif(operator == "-"):
sum = (int(num1)) - (int(num2))
length = max(len(num1), len(num2)) + 2
top = str(num1).rjust(length)
bottom = operator + str(num2).rjust(length - 1)
line = ""
res = str(sum).rjust(length)
for s in range (length):
line += "-"
if problem != problems[-1]:
first += top + ' '
second += bottom + ' '
lines += line + ' '
sumx += res + ' '
else:
first += top
second += bottom
lines += line
sumx += res
string = first + '\n' + second + '\n' + lines + '\n' + sumx
else:
string = first + '\n' + second + '\n' + lines
return string
return problems
print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 - 49"])}')
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project