Tell us what’s happening:
I have been working on this project for a while right now, and I finally got positive results by the end while debugging, and trying it myself with multiple string arrays . But still, for some reason I get three failures when I try to submit it with the inbound testing method.
I tried to read from other people’s posts in case i was doing something wrong or having a wrong understanding of the topic but I didn’t find anything wrong.
Thank you .
Your code so far
def arithmetic_arranger(problems, show_answer=False):
if len(problems) >= 6:
return 'Error: Too many problems.'
lower = ''
upper = ''
line = ''
downer = ''
answer = ''
for problem in problems:
problem.split()
first_number = problem.split()[0]
operation = problem.split()[1]
second_number = problem.split()[2]
if first_number.isnumeric() or second_number.isnumeric():
if len(first_number) >= 5 or len(second_number) >= 5:
return "Error: Numbers cannot be more than four digits."
if operation == '-' or operation == '+':
if len(str(first_number)) > len(str(second_number)):
upper += ' ' + first_number + ' '
elif len(str(first_number)) < len(str(second_number)):
upper += ' ' + (' ' * abs(len(str(first_number)) - len(str(second_number)))) + first_number + ' '
else:
upper += ' ' + first_number + ' '
if len(str(first_number)) > len(str(second_number)):
lower += operation + ' ' + (' ' * abs(len(str(first_number)) - len(str(second_number)))) + second_number + ' '
elif len(str(first_number)) < len(str(second_number)):
lower += operation + ' ' + second_number + ' '
else:
lower += operation + ' ' + second_number + ' '
line += '-' * (max(len(str(first_number)), len(str(second_number))) + 2) + ' '
if show_answer:
if operation == '-':
answer += ' ' + (' ' * (max(len(str(first_number)), len(str(second_number))) - len(str(int(first_number) + int(second_number))))) + str(int(first_number) - int(second_number)) + ' '
else:
answer += ' ' + (' ' * (max(len(str(first_number)), len(str(second_number))) - len(str(int(first_number) + int(second_number))))) + str(int(first_number) + int(second_number)) + ' '
else:
return "Error: Operator must be '+' or '-'."
else:
return "Error: Numbers must only contain digits."
if show_answer:
final = upper + '\n' + lower + '\n' + line + '\n' + answer
return final
else:
final = upper + '\n' + lower + '\n' + line
return final
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36
.
Challenge: Arithmetic Formatter
Link to the challenge: