Pretty new to Python, but I have some background in C and VB. Just joined this site because I couldn't find anywhere else to get Python help!

I'm working with VPython in the IDLE environment, and I'm having a problem changing a variable using a toggle switch and a function. Here's the necessary parts:

mode = 0
def switchMode():
    if t1.value:
        mode = 1
    else:
        mode = 0

tMoGen = toggle(pos=(40,-30),width=10,height=10,text0='Off',text1='On',action=lambda: switchMode())

while True:
    rate(5)
    c.interact()
    print mode

Mode just stays at 0, no matter how many times I flip the switch.
I think I may need to return the value out of the function, but I'm not sure how to do that yet.

Recommended Answers

All 7 Replies

You're right, you need return.

Python has local and global scopes, functions have their own local scope, so a local variable is changed inside of the function, it doesn't affect a variable outside the function's scope.

Add return mode to switchMode(),
and then whenever you call switchMode,
do this: mode = switchMode()

That was an easy fix, thanks!

The mode declared is outside the function, which makes it a GLOBAL variable.

The mode within the function is a LOCAL variable, which doesn't affect the mode that's declared outside of the function

A simple fix would be to declare that the mode inside the function is a global variable.

mode = 0
def switchMode():
    global mode # This tells python that all reference to "mode" is global within the current function.
    if t1.value:
        mode = 1
    else:
        mode = 0

The wording is probably really weird, so I'm going to try to explain via example:

var1 = 88
print var1 # 88 would be print in console
function() # Executes function(), which will error out, of course


def function():
    print var1 # This would print 88, as python didn't find a local variable
    var1 += 1 # This is suppose to add 1 to var1, however you will get "UnboundLocalError: local variable 'var1' referenced before assignment" 
    # This is because there's no local variable named "var1". Since python evaluates the right side of an equation first, and that you are creating a new local variable after the evaluation, python gets confused and errors out.
var1 = 88 # Declare this again
print var1 # will print out 88 again
function() #Executes the function
print var1 # will print out 88, not 90. You can see from this that var1 outside is completely different from the var1 that's inside function().

def function():
    var1 = 90 # You might think that you changed var1 that's declared outside of this function to 90, but really what you did is that you created a new variable within the function and set it to be 90
    print var1 # will print 90 as expected
    print 90

To fix these problems, we need the global statement

var1 = 88
print var1 # prints out 88
function() # Will not error out
print var1 # Will print out 91 this time around, instead of 88

def function():
    global var1 # Tells python that var1 referenced insdie the function is a global variable, not a local one
    var1 += 1 # adds 1 to the GLOBAL var1, which is 89
    print var1 # prints out 89
    var1 = 91 # sets the GLOBAL var1 to be 91
    print var1 # prints out 91

Never use the global function, though.
Use of it is a sign of a bad program structure.

Not sure if I agree with that. I think they are great temporary variables there's needed for multiple classes until they are passed off as an argument to something else.

It has to be made clear that global should never be used.

Whenever you see the need to write a global statement, I strongly encourage you to rewrite some parts of your code.

I do have enough background in C to know what a global variable is, I just didn't even think about that for here, nor did I know the syntax.

I've also done enough Actionscripting to know that I tend to get SERIOUSLY CONFUSED when I use too many globals, so I think I'll get into the habit of just returning a value, now that I know how to do that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.