Hello everybody,
I worked on the arithmetic arranger project, I tried on many examples and it works fine but I don’t know why doesn’t pass the unit tests, here is my code:
def arithmetic_arranger(problems, res=False):
if len(problems)>5 :
print("Error : Too many problems.")
quit() ;
op1=[]
op2=[]
pl=[]
maxlen=[]
st1=''
st2=''
st3=''
st4=''
for oper in problems :
oper.rstrip()
oper.lstrip()
l=oper.split()
#print(l[1]=="+")
if l[1]!="+":
if l[1]!="-" :
print("Error: Operator must be '+' or '-'.")
quit()
try :
x1=int(l[0])
#print(x1)
x2=int(l[2])
#print(x2)
except :
print("Error: Numbers must only contain digits")
quit()
if len(l[0])>4 or len(l[2])>4 :
print("Error: Numbers cannot be more than four digits.")
quit()
#maxlen=max([len(l[0]), len(l[2])])+2
op1.append(l[0])
op2.append(l[2])
pl.append(l[1])
maxlen.append(max([len(l[0]), len(l[2])])+2)
for i in range(len(problems)) :
nbsp1=maxlen[i]-len(op1[i])
nbsp2=maxlen[i]-len(op2[i])
st1=st1+nbsp1*" "+op1[i]+4*" "
#print(st1)
st2=st2+pl[i]+" "+(nbsp2-2)*" "+op2[i]+4*" "
#print(st2)
st3=st3+maxlen[i]*"-"+4*" "
#print(st3)
if res==True :
if pl[i]=="+" :
ress=int(op1[i])+int(op2[i])
#print(ress)
elif pl[i]=="-" :
ress=int(op1[i])-int(op2[i])
ress1=str(ress)
#print(ress1)
nbsp3=maxlen[i]-len(ress1)
st4=st4+nbsp3*" "+ress1+4*" "
#print(st4)
#print(st1)
#print(st2)
#print(st3)
#print(st4)
if res==True :
resultat=st1+"\n"+st2+"\n"+st3+"\n"+st4
#print(resultat)
else :
resultat=st1+"\n"+st2+"\n"+st3
return resultat
I would appreciate any help or indication. Thank you ^^
