Undefined is not iterable (cannot read property Symbol (Symbol.iterator))

74 views
Skip to first unread message

QUAH KAH CHUN UPSI

unread,
Dec 17, 2020, 6:36:09 AM12/17/20
to Glowscript Users
h=10
L=40
c1=color.red
r1=1
g=9.81
t=0
dt=0.0001
e=0.7
n=0
c=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)
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)

def movement(s,v,a,t):
    v+=a*t
    s+=v*t
    return(s,v)
def bounce(sy,r,gy,vy,e,nobounce,c):
    if(sy<=r+gy/2 and vy<0):
        vy=-vy*e
        nobounce+=1
        c.visible=True
        c.text=nobounce
        c.pos=b1.pos
        return(vy,nobounce,c)

while(n<5):
    rate(10000)
    T.text="T="" "+t+" ""s"
    b1.pos,b1.v=movement(b1.pos,b1.v,b1.a,dt)
    b1.v.y,n,C=bounce(b1.pos.y,r1,ground.size.y,b1.v.y,e,n,C)
    t+=dt

I want to define the function "bounce" but it appears this error, how can I fix it?

Bruce Sherwood

unread,
Dec 17, 2020, 12:14:58 PM12/17/20
to Glowscript Users
There are two problems:

1) You need an else in the bounce function. The error you see arises from returning nothing when the if is not executed.

2) You should not return C; the bounce function updates C's visible, text, and pos attributes.

Bruce

QUAH KAH CHUN UPSI

unread,
Dec 17, 2020, 8:35:46 PM12/17/20
to glowscri...@googlegroups.com
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.

--

---
You received this message because you are subscribed to the Google Groups "Glowscript Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to glowscript-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/glowscript-users/790486d7-b6de-41d1-b1bc-cb8c77ae8653n%40googlegroups.com.

Harlan Gilbert

unread,
Dec 17, 2020, 9:02:33 PM12/17/20
to glowscri...@googlegroups.com
You still need to return something in that "else" tine.



--
Harlan Gilbert
Co-Chair and Math, Physics, Computer Science, and Philosophy Teacher
Green Meadow Waldorf High School
Chestnut Ridge, NY 10977

QUAH KAH CHUN UPSI

unread,
Dec 17, 2020, 9:28:18 PM12/17/20
to glowscri...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages