3

I have code that looks similar to this

v = '0'

def program():
    x = input('1 or 2 ')
    if x == '1':
        print('it is 1')
        v = '1'
    elif x == '2':
        print('it is 2')
        v = '2'

while True:
    program()
    print(v)

However, when I run this code the variable 'v' always prints out the default 0. Why isn't it giving me the variable I assigned it in the function?

2

4 Answers 4

3

You have two variables named v:

  1. The global level v=0 declaration at the top.
  2. The function declaration of v in program.

First of all, you really shouldn't use globals in functions, as it is bad programming practice. You should pass it as a parameter and return with any other results.

If you really must, you can modify a global variable in a function by first declaring it as a global variable.

Also note that you need to use raw_input in Python 2.

def program():
    global v
    x = raw_input('1 or 2 ')
    if x == '1':
        print('it is 1')
        v = '1'
    elif x == '2':
        print('it is 2')
        v = '2'

Using global variables in a function other than the one that created them

Sign up to request clarification or add additional context in comments.

1 Comment

Nice explanation. Good call on the bad practice mention.
2

Your function manipulates a local copy of variable v. If you want to get the value of v after calling program(), append return v to the end of your function definition. That is:

v = '0'

def program():
    x = input('1 or 2 ')
    if x == '1':
        print('it is 1')
        v = '1'
    elif x == '2':
        print('it is 2')
        v = '2'
    return v

while True:
    v = program()
    print(v)

If you don't want to return anything, you can set v to the globally declared variable as so:

v = '0'

def program():
    x = input('1 or 2 ')
    if x == '1':
        print('it is 1')
        global v
        v = '1'
    elif x == '2':
        print('it is 2')
        global v
        v = '2'

while True:
    program()
    print(v)

Comments

1

To complement the duplicate flag, here is an explanation with respect to your code:

You need to explicitly tell your method that you want to use the global v, otherwise, it will never get updated from what is happening to the v within the method scope.

To rectify this, you want to add global v inside your method:

def program():
    global v
    # rest of your code here

That should work.

Comments

1

Variable assignments in Python are locally scoped. If you want to manipulate a global state (or an enclosing state) inside of a function, you can wrap that state in a holder and then reference the holder. For example:

v = ['0']

def program():
    x = input('1 or 2 ')
    if x == '1':
        print('it is 1')
        v[0] = '1'
    elif x == '2':
        print('it is 2')
        v[0] = '2'

while True:
    program()
    print(v[0])

The above segment references an array and manipulates the value inside of the array.

1 Comment

You might want to explain how one changes the actual id of the variable while the other just modifies the object that the variable is referencing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.