Linked Questions
21 questions linked to/from Why can't I set a global variable in Python?
4042 votes
27 answers
4.2m views
How can I use a global variable in a function?
How do I create or use a global variable inside a function? How do I use a global variable that was defined in one function inside other functions? Failing to use the global keyword where appropriate ...
317 votes
15 answers
120k views
UnboundLocalError trying to use a variable (supposed to be global) that is (re)assigned (even after first use)
When I try this code: a, b, c = (1, 2, 3) def test(): print(a) print(b) print(c) c += 1 test() I get an error from the print(c) line that says: UnboundLocalError: local variable 'c' ...
-2 votes
2 answers
209 views
Isn't this a proper global variable usage in python? It keeps on giving syntax error. What really is a global keyword usage [duplicate]
q=8 def example(q): global q q=q+2 print(q) example() File "<ipython-input-35-0e5cbb9ebefd>", line 2 global q ^ SyntaxError: name 'q' is parameter and global ...
-2 votes
1 answer
73 views
Using a variable inside a function without calling the function [duplicate]
I want to get a directory with a function and use the obtained path in another function. For example, I'm just giving a simple code sample. def browse_folder(): input_folder = input("enter ...
0 votes
0 answers
63 views
Python: why global variable act as local variable in method? [duplicate]
I know it is a basic question but i don't know reason of this question. I am confused a bit, why global variable is act as local variable in test method. x=1 def test(): print x x=2 ...
0 votes
0 answers
43 views
How do I add cookies to my cookie clicker game? [duplicate]
I am trying to make the worst cookie clicker game but when you click the "cookie" nothing gets added to the score. (I'm not using pygame btw) Here is the code I'm using: import tkinter from ...
0 votes
0 answers
37 views
Can't attribute an int value to a variable every time I change it via spinbox and use it in the rest of my program [duplicate]
I'm making a program that display the location of a vehicule on a map, I use "tkintermapview" to display a map, and tkinter to display stuff on it (road it's suppose to be on, icone to show ...
0 votes
0 answers
28 views
Why cant i use the autosave variable in the Following code? [duplicate]
The function works by looking at the string-value of the autosave and setting it to "yes" or "no". The programm itself seems that it wont reference to the variable. from tkinter ...
0 votes
0 answers
27 views
Prevent Python From Creating New Variable? [duplicate]
I'm working on something in python where I need to use a global variable that is changed by a function. However, when I try to change the value inside the function, python instead creates a new local ...
0 votes
0 answers
26 views
Using signal to clean up process [duplicate]
I am learning how to use signals. I created a small program that connects to a database and prints the results. I am leaving the database connection open. I am sending a SIGTERM to end the program. ...
0 votes
0 answers
26 views
modify global variable in a decorated function [duplicate]
I have a decorator which inits a global dict which I want to modify within the decorated function. I have tried an implementation something like below: def decorator(): def inner_function(func): ...
0 votes
0 answers
18 views
Python: Trying to use recursion to determine descending order, but variable is seemingly changing itself in the process [duplicate]
I'm trying to make a function that returns whether or not a List is in descending order by using recursion, but the variable descending seems to be changing from True back to False on its own. Also, I ...