Sign in to follow this  

Python: scope question

This topic is 4106 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I recently started playing around with python, but I am having some problems understanding scope rules. I want to update a variable that is outside the current scope, NOT create a new one. Example:
a = 2

while(1):
    a += 1

    if a > 50:
        a = 2

There are no problems with using 'a' inside the while loop. As far as I am aware, there is no new variable created here. However, inside the if statement, there is a new local variable 'a' created since the statement does not have any effect on the 'a' that is outside of the if statement. What can I do to change this?

Share this post


Link to post
Share on other sites
I just tried this:
a = 2

while 1:
a += 1
if a > 50:
a = 2
print a


And it worked exactly as you wanted, as it should. I happen to be using ActivePython 2.4.3, but that shouldn't make a difference. Once you've declared a variable, it shouldn't disappear until you leave its scope, which can never happen for a global variable like you declared here.

Share this post


Link to post
Share on other sites
Oh...

Then I possibly misdiagnosed the problem. I'll try to look a bit deeper. I'm using VPython to draw a sphere that accelerates in one direction and then the other. The problem is that once it reaches one edge, it simply vibrates in place forever, so I assumed that the reason was that my variable 'dt' never got reset inside my if statement because it created a duplicate instead of updating it.

If you have Python and VPython, you can try out my buggy program:


from visual import *
from visual.graph import *

# Create a sphere and give it a velocity
ball = sphere( pos=(0,4,0), color=color.red )
ball.velocity = vector(1,0,0)
dt = 0.1

# Give the ball some acceleration
a = 0.001

# Draw the axes
xaxis = arrow( pos=(0,0,0), axis=(5,0,0), shaftwidth=0.5 )
yaxis = arrow( pos=(0,0,0), axis=(0,5,0), shaftwidth=0.5 )
zaxis = arrow( pos=(0,0,0), axis=(0,0,5), shaftwidth=0.5 )

# Set up the velocity graph
displayGraph = gdisplay( x=0, y=400, width=800, height=400,
title='Position v. Time', xtitle='Time',
ytitle='Position', xmax=100., xmin=0.,
ymax=5., ymin=-5., foreground=color.black,
background=color.white )
graph = gcurve( gdisplay = displayGraph, color = color.blue )

# Move the ball and graph its position
x=0
while(1):

# Execute this loop no faster than 100 times per second
rate(100)

# Update the velocity according to the acceleration
dt += a

# Update the position of the ball based on the new velocity
ball.pos = ball.pos + ball.velocity*dt

# For graphing purposes
x += .1

if abs( ball.x ) > 4:

# Reverse the direction of the velocity
ball.velocity.x = -ball.velocity.x

# Reset the magnitude of the velocity
dt = 0.1

print 'change'

print 'normal'

graph.plot( pos=( x, ball.pos.x ) )


Share this post


Link to post
Share on other sites
First off, I'd recommend putting all your code in a main() function, then loading up the Python debugger like this:
def main():
...
...

import pdb
pdb.run('main()')




Set a breakpoint in the velocity-reversing code, then step through and print out the variables so you can see what's happening.

The problem is that ball.x reaches 4.13, and it has insufficient velocity on the first step to go below 4, so the change code gets called again. My quick solution was to do this:
	    if abs( ball.x ) > 4:

# Reverse the direction of the velocity
ball.velocity.x = -ball.velocity.x

# Reset the magnitude of the velocity
dt = 0.1

if ball.x > 0:
ball.x = 4
else:
ball.x = -4

print 'change'

Share this post


Link to post
Share on other sites

This topic is 4106 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this