How to Manipulate Variable Value inside a Function When The Variable was Declared Outside the Function?

KingMak

I would like to increase the value of variable count by 1, but only when the function printcount is called in the CODE below.

global count
count = 0

def PrintCount():
    count += 1
    print count

PrintCount()

When I run this code I get the following error:

Traceback (most recent call last):
  File "C:\Users\Cross\Desktop\Code.py", line 8, in <module>
    PrintCount()
  File "C:\Users\Cross\Desktop\Code.py", line 5, in PrintCount
    count += 1
UnboundLocalError: local variable 'count' referenced before assignment

I would like to know why this is happening and how to fix it.

Thank You.

falsetru

Use global keyword inside the function to access (especially modify) the global variable count:

def PrintCount():
    global count # <----
    count += 1
    print count

Declaring global outside the function has no effect.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Will a variable declared with cdef outside a function have the same type inside the function?

From Dev

What is the value of the static variable declared inside a function?

From Dev

Use a variable declared in a function outside that function

From Dev

When does a variable need to be declared outside a function in Javascript?

From Dev

How to make a variable inside a function be available outside?

From Dev

How to change the value of a variable that is declared out of a function?

From Dev

How to capture current value of an outside variable when function is defined

From Dev

How to get variable value outside a function IN jquery

From Dev

How to assign value to variable outside the function in $on AngularJS?

From Dev

How to get variable value outside angular function

From Dev

Variable not retaining value outside of function

From Dev

Retrieve value of variable outside function

From Dev

accessing variable value outside of function

From Dev

Setting a value in a function as a variable outside the function (Swift)

From Dev

How/what is the value of a variable in a nested function declared nonlocal set to?

From Dev

array variable inside function affects the array variable outside the function in c

From Dev

array variable inside function affects the array variable outside the function in c

From Dev

Manipulate a local static pointer variable outside the function where it is defined

From Dev

Manipulate a local static pointer variable outside the function where it is defined

From Dev

how/when is a static variable in a function declared within C language?

From Dev

bash function - Get function stdout value inside a variable and modify variables outside the function

From Dev

Calling a JavaScript variable inside a function from outside

From Dev

static variable scope inside and outside the function

From Dev

Permanently changing an outside variable inside a function

From Dev

How is it possible to reference to a variable outside of a code block when it is declared inside a block?

From Dev

Change the value of a variable inside a function

From Dev

Manipulate the Value of Variable inside String

From Dev

Unable to reference one particular variable declared outside a function

From Dev

Global variable declared outside function doesn't work

Related Related

  1. 1

    Will a variable declared with cdef outside a function have the same type inside the function?

  2. 2

    What is the value of the static variable declared inside a function?

  3. 3

    Use a variable declared in a function outside that function

  4. 4

    When does a variable need to be declared outside a function in Javascript?

  5. 5

    How to make a variable inside a function be available outside?

  6. 6

    How to change the value of a variable that is declared out of a function?

  7. 7

    How to capture current value of an outside variable when function is defined

  8. 8

    How to get variable value outside a function IN jquery

  9. 9

    How to assign value to variable outside the function in $on AngularJS?

  10. 10

    How to get variable value outside angular function

  11. 11

    Variable not retaining value outside of function

  12. 12

    Retrieve value of variable outside function

  13. 13

    accessing variable value outside of function

  14. 14

    Setting a value in a function as a variable outside the function (Swift)

  15. 15

    How/what is the value of a variable in a nested function declared nonlocal set to?

  16. 16

    array variable inside function affects the array variable outside the function in c

  17. 17

    array variable inside function affects the array variable outside the function in c

  18. 18

    Manipulate a local static pointer variable outside the function where it is defined

  19. 19

    Manipulate a local static pointer variable outside the function where it is defined

  20. 20

    how/when is a static variable in a function declared within C language?

  21. 21

    bash function - Get function stdout value inside a variable and modify variables outside the function

  22. 22

    Calling a JavaScript variable inside a function from outside

  23. 23

    static variable scope inside and outside the function

  24. 24

    Permanently changing an outside variable inside a function

  25. 25

    How is it possible to reference to a variable outside of a code block when it is declared inside a block?

  26. 26

    Change the value of a variable inside a function

  27. 27

    Manipulate the Value of Variable inside String

  28. 28

    Unable to reference one particular variable declared outside a function

  29. 29

    Global variable declared outside function doesn't work

HotTag

Archive