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?

Jopplk
  • 35
  • 6

4 Answers4

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

Community
  • 1
  • 1
Alexander
  • 105,104
  • 32
  • 201
  • 196
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)
Andriko13
  • 992
  • 1
  • 7
  • 34
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.

idjaw
  • 25,487
  • 7
  • 64
  • 83
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.

squid314
  • 1,394
  • 8
  • 9
  • 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. – pzp Mar 02 '16 at 00:35