GlowScript 3.0 VPython
h=10
L=40
r1,c1=1,color.red
r2,c2=1,color.blue
g=9.81
t=0
dt=0.0001
e=0.7
n=0
c=0
i=0
world=canvas(width=600,height=600,center=vec(0,h/2,0),color=color.black)
ground=box(size=vec(L,2,L/4),pos=vec(0,0,0),color=color.blue)
b1=sphere(radius=r1,pos=vec(0,h,0),color=c1)
b1.v,b1.a=vec(0,0,0),vec(0,-g,0)
b2=sphere(radius=r2,pos=vec(5,2*h,0),color=c2)
b2.v,b2.a=vec(0,0,0),vec(0,-g,0)
T=label(text="T="" "+t+" ""s",pos=vec(L/3,2*h,0))
N=label(text="n=5",pos=vec(-L/3,2*h,0))
C=label(text=n,pos=b1.pos,xoffset=30,yoffset=30,visible=False)
I=label(text=i,pos=b2.pos,xoffset=30,yoffset=30,visible=False)
def movement(s,v,a,t):
v+=a*t
s+=v*t
return(s,v)
def bounce(sy,r,gy,vy,e,nobounce):
if(sy<=r+gy/2 and vy<0):
vy=-vy*e
nobounce+=1
return(vy,nobounce)
else:
nobounce=nobounce
while(n<5 or i<5):
rate(10000)
T.text="T="" "+t+" ""s"
b1.pos,b1.v=movement(b1.pos,b1.v,b1.a,dt)
b2.pos,b2.v=movement(b2.pos,b2.v,b2.a,dt)
b1.v.y,n=bounce(b1.pos.y,r1,ground.size.y,b1.v.y,e,n)
b2.v.y,n=bounce(b2.pos.y,r2,ground.size.y,b2.v.y,e,n)
t+=dt
What I have done for the problems:
1) I added else:nobounce=nobounce in the bounce function
2) I have removed the C label from the bounce function temporarily
However, the error still appears somehow.