Tell us what’s happening:
I don’t know where my mistake is. The result is correct; I’ve even made sure that the print matches what the question expects. Is there something I’m not seeing?
Your code so far
def arithmetic_arranger(problems, show_answers=False):
problema=''
res=True
resultado=''
if len(problems)>5:
problema = 'Error: Too many problems.'
return problema
res=False
for op in problems:
if op.find('/')>0 or op.find('*')>0:
problema = "Error: Operator must be '+' or '-'."
return problema
res=False
op1 = op.split()
for num in op1:
if not num.isdigit() and not (num == '+' or num == '-'):
problema = 'Error: Numbers must only contain digits.'
return problema
res=False
elif len(num)>4:
res=False
problema = 'Error: Numbers cannot be more than four digits.'
return problema
for i in problems:
b=i.split()
if len(b[0])>len(b[2]):
max_length=len(b[0])
resultado+=" {b[0]:>{max_length}}".format(b=b,max_length=max_length+1) + " "
else:
max_length=len(b[2])
resultado+=" {b[0]:>{max_length}}".format(b=b,max_length=max_length+1) + " "
resultado+='\n'
for i in problems:
b=i.split()
if len(b[0])>len(b[2]):
max_length=len(b[0])
resultado+="{b[1]} {b[2]:>{max_length}}".format(b=b,max_length=max_length) + " "
else:
max_length=len(b[2])
resultado+="{b[1]} {b[2]:>{max_length}}".format(b=b,max_length=max_length) + " "
resultado+='\n'
for i in problems:
b=i.split()
if len(b[0])>len(b[2]):
max_length=len(b[0])
resultado+='-'*(max_length+2)+ " "
else:
max_length=len(b[2])
resultado+='-' * (max_length + 2) + " "
resultado+='\n'
if res and show_answers:
for i in problems:
resu=0
b=i.split()
for j in range(0,3,2):
if (b[1])=='+':
resu+=int(b[j])
else:
if j==0:
resu+=int(b[j])
else:
resu-=int(b[j])
resu=str(resu)
if len(b[0])>len(b[2]):
max_length=len(b[0])
else:
max_length=len(b[2])
resultado+=" {resu:>{max_length}}".format(resu=resu, max_length=max_length+1) + " "
return resultado
else:
return resultado
print(f'{arithmetic_arranger(["3 + 855", "988 + 40"], True)}')
print(f' 3 988\n+ 855 + 40\n----- -----\n 858 1028')
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project