Tell us what’s happening:
the code works. when the function is called the right output is displayed in the preview. Nevertheless, when running the test all the instances fail.
Your code so far
def arithmetic_arranger (problems, show_answers=False):
if len(problems)>5:
print ('Error: Too many problems.')
return
first_lines=[]
second_lines=[]
dashes=[]
results=[]
for prob in problems:
if '*' in prob:
print ("Error: Operator must be '+' or '-'.")
return
elif '/' in prob:
print ("Error: Operator must be '+' or'-'.")
return
elif any(char.isalpha()for char in prob):
print ('Error: Numbers must only contain digits.')
return
else:
for operator in ['+','-']:
if operator in prob:
num1,operator,num2=prob.partition(operator)
num1=num1.strip()
num2=num2.strip()
if len(num1)>4 or len(num2)>4:
print('Error: Numbers cannot be more than four digits.')
return
elif operator == '+':
result_of_addition=(int(num1)+int(num2))
result=str(result_of_addition)
elif operator == '-':
result_of_subtraction=int(num1)-int(num2)
result=str(result_of_subtraction)
if len(num1)>len(num2):
adjust=len(num1)+2
first_line=str(num1).rjust(adjust)
second_line=((operator)+(" "*(len(num1)-len(num2)+1))+(num2)).rjust(adjust)
dash= '-'*(len(num1)+2)
final_result = result.rjust(adjust)
first_lines.append(first_line)
second_lines.append(second_line)
dashes.append(dash)
results.append(final_result)
elif len(num2)>=len(num1):
adjust=len(num2)+2
first_line=str(num1).rjust(adjust)
second_line=((operator)+(" ") + (num2))
dash= '-'*(len(num2)+2)
final_result=result.rjust(adjust)
first_lines.append (first_line)
second_lines.append (second_line)
dashes.append(dash)
results.append(final_result)
print (" ".join(first_lines))
print (" ".join(second_lines))
print (" ".join(dashes))
if show_answers:
print (" ".join(results))
return problems
arithmetic_arranger(["3801 - 2", "123 + 49"])
arithmetic_arranger(["1 + 2", "1 - 9380"])
arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])
arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"])
arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"])
arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"])
arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"])
arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])
arithmetic_arranger(["3 + 855", "988 + 40"], True)
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/134.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project