Accessing a variable from a defined function in a while loop

Jopplk

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?

Alexander

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Accessing a variable from a defined function in a while loop

From Dev

passing a variable from a function defined outside the loop but called inside that loop

From Dev

Variable not defined in while loop in python?

From Dev

Variable defined in while loop cannot be defined

From Dev

Accessing global variable defined inside a function

From Dev

Accessing global variable defined inside a function

From Dev

Accessing variable from different function

From Dev

Accessing a variable from outside the function?

From Dev

How to exit a while loop when the input or variable in a user-defined function meets the condition in Python

From Dev

For and while loop not working in a globally defined function

From Dev

accessing a member of a struct variable from a function variable

From Dev

Accessing a function's variable from another function

From Dev

why an variable defined in a do while loop Cannot be resolved to a variable

From Dev

accessing a variable from outside the function in python

From Dev

C++ accessing variable from other function

From Dev

accessing a variable from outside the function in python

From Dev

PHP accessing a variable from another function

From Dev

Accessing a function variable from another file in python

From Dev

Accessing outer loop variable from inside loop in Java

From Dev

How to define a variable when we defined it in a while loop?

From Dev

Accessing variable defined in slickgrid in AngularJs

From Dev

Using a defined variable from outside the current function

From Dev

accessing a variable from outside a function and returning the variable. In a module pattern

From Dev

Expected declaration while accessing global variable from second view controller

From Dev

i not defined in while loop

From Dev

accessing a function that is defined after main

From Dev

JavaScript variable is not defined at onclick button while calling a function

From Dev

Accessing array index variable from bash shell script loop?

From Dev

Accessing array index variable from bash shell script loop

Related Related

  1. 1

    Accessing a variable from a defined function in a while loop

  2. 2

    passing a variable from a function defined outside the loop but called inside that loop

  3. 3

    Variable not defined in while loop in python?

  4. 4

    Variable defined in while loop cannot be defined

  5. 5

    Accessing global variable defined inside a function

  6. 6

    Accessing global variable defined inside a function

  7. 7

    Accessing variable from different function

  8. 8

    Accessing a variable from outside the function?

  9. 9

    How to exit a while loop when the input or variable in a user-defined function meets the condition in Python

  10. 10

    For and while loop not working in a globally defined function

  11. 11

    accessing a member of a struct variable from a function variable

  12. 12

    Accessing a function's variable from another function

  13. 13

    why an variable defined in a do while loop Cannot be resolved to a variable

  14. 14

    accessing a variable from outside the function in python

  15. 15

    C++ accessing variable from other function

  16. 16

    accessing a variable from outside the function in python

  17. 17

    PHP accessing a variable from another function

  18. 18

    Accessing a function variable from another file in python

  19. 19

    Accessing outer loop variable from inside loop in Java

  20. 20

    How to define a variable when we defined it in a while loop?

  21. 21

    Accessing variable defined in slickgrid in AngularJs

  22. 22

    Using a defined variable from outside the current function

  23. 23

    accessing a variable from outside a function and returning the variable. In a module pattern

  24. 24

    Expected declaration while accessing global variable from second view controller

  25. 25

    i not defined in while loop

  26. 26

    accessing a function that is defined after main

  27. 27

    JavaScript variable is not defined at onclick button while calling a function

  28. 28

    Accessing array index variable from bash shell script loop?

  29. 29

    Accessing array index variable from bash shell script loop

HotTag

Archive