Trying to understand variable scope in python [duplicate]
This question already has an answer here:
How to change a variable after it is already defined?
6 answers
I gave this following program in Python just to understand scopes of variables, but I'm getting the error:
count = 0
def hello_func():
if(count == 5):
return 0
print("Hello")
count+=1
hello_func()
hello_func()
Error :
UnboundLocalError: local variable 'count' referenced before assignment
Can you explain what I'm doing wrong? And how do I declare count has global variable without altering the structure of the above program?
python
marked as duplicate by Community♦ Nov 11 '18 at 8:14
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How to change a variable after it is already defined?
6 answers
I gave this following program in Python just to understand scopes of variables, but I'm getting the error:
count = 0
def hello_func():
if(count == 5):
return 0
print("Hello")
count+=1
hello_func()
hello_func()
Error :
UnboundLocalError: local variable 'count' referenced before assignment
Can you explain what I'm doing wrong? And how do I declare count has global variable without altering the structure of the above program?
python
marked as duplicate by Community♦ Nov 11 '18 at 8:14
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How to change a variable after it is already defined?
6 answers
I gave this following program in Python just to understand scopes of variables, but I'm getting the error:
count = 0
def hello_func():
if(count == 5):
return 0
print("Hello")
count+=1
hello_func()
hello_func()
Error :
UnboundLocalError: local variable 'count' referenced before assignment
Can you explain what I'm doing wrong? And how do I declare count has global variable without altering the structure of the above program?
python
This question already has an answer here:
How to change a variable after it is already defined?
6 answers
I gave this following program in Python just to understand scopes of variables, but I'm getting the error:
count = 0
def hello_func():
if(count == 5):
return 0
print("Hello")
count+=1
hello_func()
hello_func()
Error :
UnboundLocalError: local variable 'count' referenced before assignment
Can you explain what I'm doing wrong? And how do I declare count has global variable without altering the structure of the above program?
This question already has an answer here:
How to change a variable after it is already defined?
6 answers
python
python
asked Nov 11 '18 at 8:10
Maccen WrightMaccen Wright
3311420
3311420
marked as duplicate by Community♦ Nov 11 '18 at 8:14
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Community♦ Nov 11 '18 at 8:14
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
When using global variables within a function, you need to declare it explicitly:
count = 0
def hello_func():
global count
if(count == 5):
return 0
print("Hello")
count+=1
hello_func()
hello_func()
The global count
in the third line declares I am using the global variable.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
When using global variables within a function, you need to declare it explicitly:
count = 0
def hello_func():
global count
if(count == 5):
return 0
print("Hello")
count+=1
hello_func()
hello_func()
The global count
in the third line declares I am using the global variable.
add a comment |
When using global variables within a function, you need to declare it explicitly:
count = 0
def hello_func():
global count
if(count == 5):
return 0
print("Hello")
count+=1
hello_func()
hello_func()
The global count
in the third line declares I am using the global variable.
add a comment |
When using global variables within a function, you need to declare it explicitly:
count = 0
def hello_func():
global count
if(count == 5):
return 0
print("Hello")
count+=1
hello_func()
hello_func()
The global count
in the third line declares I am using the global variable.
When using global variables within a function, you need to declare it explicitly:
count = 0
def hello_func():
global count
if(count == 5):
return 0
print("Hello")
count+=1
hello_func()
hello_func()
The global count
in the third line declares I am using the global variable.
answered Nov 11 '18 at 8:13
DinariDinari
1,619522
1,619522
add a comment |
add a comment |