Thank you all for your help! I’ve finally completed it!
As pointed out by Brain150, the inconsistency in line 81 was a type error.
The first problem was the script printed the ‘too many problems’ error when there were more than 4 problems, when it was supposed to be more than 5. My misunderstanding of the instructions.
Second, the amount of dashes were mathematically inconsistent when changing up the numbers. I needed it to line up with the bigger number, and not the answer. Because the answer may have more digits than the comprising numbers.
def arithmetic_arranger(problems,v=None):
n0=[]
op=[]
n1=[]
da=[]
an=[]
for p in problems:
sprob=p.split(' ')
n0.append(sprob[0])
n1.append(sprob[2])
if len(sprob[0])>4 or len(sprob[2])>4:
return "Error: Numbers cannot be more than four digits."
if '+' in sprob[1]:
try:
answer=int(sprob[0])+int(sprob[2])
except:
return "Error: Numbers must only contain digits."
an.append(str(answer))
op.append('+')
elif '-' in sprob[1]:
try:
answer=int(sprob[0])-int(sprob[2])
except:
return "Error: Numbers must only contain digits."
an.append(str(answer))
op.append('-')
else:
return "Error: Operator must be '+' or '-'."
if int(sprob[0])>int(sprob[2]):
dash='-'*(len(sprob[0])+2)
da.append(dash)
elif int(sprob[0])<int(sprob[2]):
dash='-'*(len(sprob[2])+2)
da.append(dash)
jn0=' '.join(n0)
jop=' '.join(op)
jn1=' '.join(n1)
jda=' '.join(da)
jan=' '.join(an)
sp4=' '*4
if len(problems)==1:
arranged_problems=\
jn0.rjust(len(jda))+'\n'+\
jop+jn1.rjust(len(jda)-1)+'\n'+\
jda
if v==True:
arranged_problems+-'\n'+jan.rjust(len(jda))
elif len(problems)==2:
n0a,n0b=jn0.split(' ')
opa,opb=jop.split(' ')
n1a,n1b=jn1.split(' ')
daa,dab=jda.split(' ')
ana,anb=jan.split(' ')
arranged_problems=\
n0a.rjust(len(daa))+sp4+n0b.rjust(len(dab))+'\n'+\
opa+n1a.rjust(len(daa)-1)+sp4+opb+n1b.rjust(len(dab)-1)+'\n'+\
daa+sp4+dab
if v==True:
arranged_problems+='\n'+ana.rjust(len(daa))+sp4+anb.rjust(len(dab))
elif len(problems)==3:
n0a,n0b,n0c=jn0.split(' ')
opa,opb,opc=jop.split(' ')
n1a,n1b,n1c=jn1.split(' ')
daa,dab,dac=jda.split(' ')
ana,anb,anc=jan.split(' ')
arranged_problems=\
n0a.rjust(len(daa))+sp4+n0b.rjust(len(dab))+sp4+n0c.rjust(len(dac))+'\n'+\
opa+n1a.rjust(len(daa)-1)+sp4+opb+n1b.rjust(len(dab)-1)+sp4+opc+n1c.rjust(len(dac)-1)+'\n'+\
daa+sp4+dab+sp4+dac
if v==True:
arranged_problems+='\n'+ana.rjust(len(daa))+sp4+anb.rjust(len(dab))+sp4+anc.rjust(len(dac))
elif len(problems)==4:
n0a,n0b,n0c,n0d=jn0.split(' ')
opa,opb,opc,opd=jop.split(' ')
n1a,n1b,n1c,n1d=jn1.split(' ')
daa,dab,dac,dad=jda.split(' ')
ana,anb,anc,anD=jan.split(' ')
arranged_problems=\
n0a.rjust(len(daa))+sp4+n0b.rjust(len(dab))+sp4+n0c.rjust(len(dac))+sp4+n0d.rjust(len(dad))+'\n'+\
opa+n1a.rjust(len(daa)-1)+sp4+opb+n1b.rjust(len(dab)-1)+sp4+opc+n1c.rjust(len(dac)-1)+sp4+opd+n1d.rjust(len(dad)-1)+'\n'+\
daa+sp4+dab+sp4+dac+sp4+dad
if v==True:
arranged_problems+='\n'+ana.rjust(len(daa))+sp4+anb.rjust(len(dab))+sp4+anc.rjust(len(dac))+sp4+anD.rjust(len(dad))
elif len(problems)==5:
n0a,n0b,n0c,n0d,n0e=jn0.split(' ')
opa,opb,opc,opd,ope=jop.split(' ')
n1a,n1b,n1c,n1d,n1e=jn1.split(' ')
daa,dab,dac,dad,dae=jda.split(' ')
ana,anb,anc,anD,ane=jan.split(' ')
arranged_problems=\
n0a.rjust(len(daa))+sp4+n0b.rjust(len(dab))+sp4+n0c.rjust(len(dac))+sp4+n0d.rjust(len(dad))+sp4+n0e.rjust(len(dae))+'\n'+\
opa+n1a.rjust(len(daa)-1)+sp4+opb+n1b.rjust(len(dab)-1)+sp4+opc+n1c.rjust(len(dac)-1)+sp4+opd+n1d.rjust(len(dad)-1)+sp4+ope+n1e.rjust(len(dae)-1)+'\n'+\
daa+sp4+dab+sp4+dac+sp4+dad+sp4+dae
if v==True:
arranged_problems+='\n'+ana.rjust(len(daa))+sp4+anb.rjust(len(dab))+sp4+anc.rjust(len(dac))+sp4+anD.rjust(len(dad))+sp4+ane.rjust(len(dae))
else:
return "Error: Too many problems."
return arranged_problems