Skip to main content
reclose under the new system; switch to code fences; minimize MRE; ask more explciitly
Source Link
Karl Knechtel
  • 61.3k
  • 14
  • 131
  • 193

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

I have the following scriptFrom this code:

COUNT = 0

def increment():
    COUNT = COUNT+1COUNT + 1

increment()

print COUNT

I just want to increment global variable COUNT, but this gives meget the following error:

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    increment()
  File "test.py", line 4, in increment
    COUNT = COUNT+1
UnboundLocalError: local variable 'COUNT' referenced before assignment

Why is it so? How can I increment the global variable COUNT from inside the function?

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

I have the following script:

COUNT = 0

def increment():
    COUNT = COUNT+1

increment()

print COUNT

I just want to increment global variable COUNT, but this gives me the following error:

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    increment()
  File "test.py", line 4, in increment
    COUNT = COUNT+1
UnboundLocalError: local variable 'COUNT' referenced before assignment

Why is it so?

From this code:

COUNT = 0

def increment():
    COUNT = COUNT + 1

increment()

I get the following error:

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    increment()
  File "test.py", line 4, in increment
    COUNT = COUNT+1
UnboundLocalError: local variable 'COUNT' referenced before assignment

Why? How can I increment the global variable COUNT from inside the function?

Post Closed as "Duplicate" by Karl Knechtel python
Post Reopened by Karl Knechtel python
deleted 29 characters in body
Source Link
ROMANIA_engineer
  • 57k
  • 30
  • 211
  • 207

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

Hello Guys! I have the following script:

COUNT = 0

def increment():
    COUNT = COUNT+1

increment()

print COUNT

I just want to increment global variable COUNT, but this gives me the following error:

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    increment()
  File "test.py", line 4, in increment
    COUNT = COUNT+1
UnboundLocalError: local variable 'COUNT' referenced before assignment

Can anyone explain me why itWhy is it so?

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

Hello Guys! I have the following script:

COUNT = 0

def increment():
    COUNT = COUNT+1

increment()

print COUNT

I just want to increment global variable COUNT, but this gives me the following error:

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    increment()
  File "test.py", line 4, in increment
    COUNT = COUNT+1
UnboundLocalError: local variable 'COUNT' referenced before assignment

Can anyone explain me why it is so?

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

I have the following script:

COUNT = 0

def increment():
    COUNT = COUNT+1

increment()

print COUNT

I just want to increment global variable COUNT, but this gives me the following error:

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    increment()
  File "test.py", line 4, in increment
    COUNT = COUNT+1
UnboundLocalError: local variable 'COUNT' referenced before assignment

Why is it so?

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

Possible Duplicate:
Using global variables in a function other than the one that created themUsing global variables in a function other than the one that created them

Hello Guys! I have the following script:

COUNT = 0

def increment():
    COUNT = COUNT+1

increment()

print COUNT

I just want to increment global variable COUNT, but this gives me the following error:

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    increment()
  File "test.py", line 4, in increment
    COUNT = COUNT+1
UnboundLocalError: local variable 'COUNT' referenced before assignment

Can anyone explain me why it is so?

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

Hello Guys! I have the following script:

COUNT = 0

def increment():
    COUNT = COUNT+1

increment()

print COUNT

I just want to increment global variable COUNT, but this gives me the following error:

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    increment()
  File "test.py", line 4, in increment
    COUNT = COUNT+1
UnboundLocalError: local variable 'COUNT' referenced before assignment

Can anyone explain me why it is so?

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

Hello Guys! I have the following script:

COUNT = 0

def increment():
    COUNT = COUNT+1

increment()

print COUNT

I just want to increment global variable COUNT, but this gives me the following error:

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    increment()
  File "test.py", line 4, in increment
    COUNT = COUNT+1
UnboundLocalError: local variable 'COUNT' referenced before assignment

Can anyone explain me why it is so?

insert duplicate link
Source Link
Loading
Post Closed as "exact duplicate" by Kev
Source Link
user873286
  • 8.2k
  • 7
  • 32
  • 39
Loading