The code i wrote for arithmetic calculator in python simply returns 2 errors.
one saying that its not formatted well, although it is well formatted.
other saying that the soultion is incorrect while it is correct too.
im completely new to programming and the code i have written is pretty long and and immature. But it works tho.
Pls help me fix the 2 errors.
Thanks in advance…
Here is the code
def arithmetic_arranger(problems,a=False):
# no. of problems test
if len(problems)>5: # if greater than 5
x='Error: Too many problems.'
return x
# if no. test passed we go through here
else:
# goin through the problems for operator test
for i in problems:# tearing apart
if '+' not in i:# test if +
if '-' not in i:# test if -
x="Error: Operator must be '+' or '-'."
return x
# test if they r actually numbers
for i in problems:# loop through them
stuff=i.split()#tear em apart into stuf
try:# stupidest method to test, but it works
number1=int(stuff[0])# test if it blows up
number2=int(stuff[2])# test if it blows up
except:# if it blew up
x="Error: Numbers must only contain digits."
return x
#test for length of digits
for i in problems:#loop through the problems
stuff=i.split()# tear em in
if len(stuff[0])>4 or len(stuff[2])>4:#test it
x="Error: Numbers cannot be more than four digits."
return x# if it blew up
#tests over.....lets start the work
op1=[]#operator one memory here
op2=[]#operator two memory here
op=[]#the sign is stored here
for i in problems:#loop through the problems
stuff=i.split()#tear em
op1.append(stuff[0])#put operator 1 in place
op2.append(stuff[2])#put operator 2 in place
op.append(stuff[1])#put sign in place
#secondline setup
s=0
x=0
secondline=''
for i in op:
while s<len(op2):
if len(op1[s])>len(op2[s]):
x=len(op1[s])
elif len(op2[s])>len(op1[s]):
x=len(op2[s])
elif len(op1[s])==len(op2[s]):
x=len(op1[s])
if x==4 and len(op2[s])==1:
secondline=secondline+' '
if x==3 and len(op2[s])==1:
secondline=secondline+' '
if x==2 and len(op2[s])==1:
secondline=secondline+' '
if x==1 and len(op2[s])==1:
secondline=secondline+''
if x==4 and len(op2[s])==2:
secondline=secondline+' '
if x==3 and len(op2[s])==2:
secondline=secondline+' '
if x==2 and len(op2[s])==2:
secondline=secondline+''
if x==4 and len(op2[s])==3:
secondline=secondline+' '
if x==3 and len(op2[s])==3:
secondline=secondline+''
ss=' '
s4=' '
if True:
if len(op1[s])-len(op2[s])==3:
ss=ss+' '
secondline=secondline+'\b\b\b'
elif len(op1[s])-len(op2[s])==2:
ss=ss+' '
secondline=secondline+'\b\b'
elif len(op1[s])-len(op2[s])==1:
ss=ss+' '
secondline=secondline+'\b'
secondline=secondline+op[s]+ss+op2[s]+s4
s+=1
#first line setup
firstline=''
s=0
space1=''
while s<len(op1):
if len(op1[s])>len(op2[s]):
x=len(op1[s])
elif len(op2[s])>len(op1[s]):
x=len(op2[s])
elif len(op1[s])==len(op2[s]):
x=len(op1[s])
if x==4 and len(op1[s])==1:
firstline=firstline+' '
if x==4 and len(op1[s])==2:
firstline=firstline+' '
if x==4 and len(op1[s])==3:
firstline=firstline+' '
if x==4 and len(op1[s])==4:
firstline=firstline+' '
if x==3 and len(op1[s])==1:
firstline=firstline+' '
if x==3 and len(op1[s])==2:
firstline=firstline+' '
if x==3 and len(op1[s])==3:
firstline=firstline+' '
if x==2 and len(op1[s])==1:
firstline=firstline+' '
if x==2 and len(op1[s])==2:
firstline=firstline+' '
if x==1 and len(op1[s])==1:
firstline=firstline+' '
i=op1[s]
firstline=firstline+space1
firstline=firstline+i
firstline=firstline+' '
s+=1
#third line setup
thirdline=''
s=0
x=0
distance=[]
for i in op2:
if len(op1[s])>len(op2[s]):
x=len(op1[s])
elif len(op2[s])>len(op1[s]):
x=len(op2[s])
elif len(op1[s])==len(op2[s]):
x=len(op1[s])
if x==1:
line='---'
distance.append(3)
elif x==2:
line='----'
distance.append(4)
elif x==3:
line='-----'
distance.append(5)
elif x==4:
line='------'
distance.append(6)
s+=1
thirdline=thirdline+line+' '
# solution line setup
sol=[]
if a:
s=0
while s<len(op1):
if op[s]=='+':
solution=int(op1[s])+int(op2[s])
elif op[s]=='-':
solution=int(op1[s])-int(op2[s])
solution=str(solution)
if len(solution)==5 and distance[s]==6:
sol.append(' ')
if len(solution)==4 and distance[s]==6:
sol.append(' ')
if len(solution)==3 and distance[s]==6:
sol.append(' ')
if len(solution)==2 and distance[s]==6:
sol.append(' ')
if len(solution)==1 and distance[s]==6:
sol.append(' ')
if len(solution)==5 and distance[s]==5:
sol.append('')
if len(solution)==4 and distance[s]==5:
sol.append(' ')
if len(solution)==3 and distance[s]==5:
sol.append(' ')
if len(solution)==2 and distance[s]==5:
sol.append(' ')
if len(solution)==1 and distance[s]==5:
sol.append(' ')
if len(solution)==4 and distance[s]==4:
sol.append('')
if len(solution)==3 and distance[s]==4:
sol.append(' ')
if len(solution)==2 and distance[s]==4:
sol.append(' ')
if len(solution)==1 and distance[s]==4:
sol.append(' ')
if len(solution)==3 and distance[s]==3:
sol.append('')
if len(solution)==2 and distance[s]==3:
sol.append(' ')
if len(solution)==1 and distance[s]==3:
sol.append(' ')
sol.append(solution)
sol.append(' ')
s+=1
if a:
solutionline=''
for i in sol:
solutionline=solutionline+str(i)
if a:
arithmetic=firstline.rstrip()+'\n'+secondline.rstrip()+'\n'+thirdline.rstrip()+'\n'+solutionline.rstrip()
else:
arithmetic=firstline.rstrip()+'\n'+secondline.rstrip()+'\n'+thirdline.rstrip()
return arithmetic
Thanks in advance…