Tell us what’s happening:
My code passes all of the error message tests but only one of the formatting tests. As far as I can tell, when manually inputting the input as the test would do it, it produces the correct formatting, but when the test tries it it fails. Am I missing certain things to make it consistent?
Your code so far
def arithmetic_arranger(problems, show_answers=False):
arranged_problems=[]
tops=[]
ops=[]
bottoms=[]
ds=[]
answers=[]
t1=''
op1=''
b1=''
d1=''
ans1=''
t2=''
op2=''
b2=''
d2=''
ans2=''
t3=''
op3=''
b3=''
d3=''
ans3=''
t4=''
op4=''
b4=''
d4=''
ans4=''
t5=''
op5=''
b5=''
d5=''
ans5=''
final_arrangement=''
if len(problems)>5:
return 'Error: Too many problems.'
else:
pass
for char in problems:
top_space=0
bottom_space=0
exp=char.split(" ")
# exp=["num1""op""num2"]
max_length=(max(len(str(exp[0])),len(str(exp[2]))))
if max_length>4:
return ('Error: Numbers cannot be more than four digits.')
try:
int(exp[0])
except:
return ('Error: Numbers must only contain digits.')
try:
int(exp[2])
except:
return ('Error: Numbers must only contain digits.')
if exp[1] not in ["+","-"]:
return ("Error: Operator must be '+' or '-'.")
exp=char.split(" ")
top=exp[0].rjust(max_length+2)
op=exp[1]
bottom=exp[2].rjust(max_length+1)
d="-"*(max_length+2)
if show_answers:
if op=='+':
ans=int(top)+int(bottom)
else:
ans=int(top)-int(bottom)
ans=str(ans).rjust(max_length+2)
else:
ans=''
tops.append(top)
ops.append(op)
bottoms.append(bottom)
ds.append(d)
answers.append(ans)
try:
tops[0]
except:
pass
else:
t1=tops[0]+' '
op1=ops[0]
b1=bottoms[0]+' '
d1=ds[0]+' '
ans1=answers[0]+' '
try:
tops[1]
except:
pass
else:
t2=tops[1]+' '
op2=ops[1]
b2=bottoms[1]+' '
d2=ds[1]+' '
ans2=answers[1]+' '
try:
tops[2]
except:
pass
else:
t3=tops[2]+' '
op3=ops[2]
b3=bottoms[2]+' '
d3=ds[2]+' '
ans3=answers[2]+' '
try:
tops[3]
except:
pass
else:
t4=tops[3]+' '
op4=ops[3]
b4=bottoms[3]+' '
d4=ds[3]+' '
ans4=answers[3]+' '
try:
tops[4]
except:
pass
else:
t5=tops[4]
op5=ops[4]
b5=bottoms[4]
d5=ds[4]
ans5=answers[4]
final_arrangement=(f'{t1}{t2}{t3}{t4}{t5}\n{op1}{b1}{op2}{b2}{op3}{b3}{op4}{b4}{op5}{b5}\n{d1}{d2}{d3}{d4}{d5}\n{ans1}{ans2}{ans3}{ans4}{ans5}')
return final_arrangement
print(f'\n{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/126.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project