Linked Questions
22 questions linked to/from Function not changing global variable
2
votes
2
answers
7k
views
Python: How to increment a global variable using a function [duplicate]
counter = 0
def addCounter():
counter = counter + 1
return counter
UnboundLocalError: local variable 'counter' referenced before assignment.
I'm trying to make a counter count every time ...
-2
votes
2
answers
2k
views
How to define empty variable in python or pass value from a function to global variable? [duplicate]
I am making some kind of basic image filter app. I have a function to open and initialize image but variables stay just in function and I can't get them from another function, so I need to define ...
1
vote
1
answer
3k
views
Python will not change a boolean value after it runs through a function [duplicate]
from random import randrange
on = ''
max_number = 0
random_number = 0
def start():
max_number = int(input('Enter max generated number: '))
random_number = randrange(max_number)
print(...
-1
votes
3
answers
2k
views
Use of Global Variables in Python? [duplicate]
I could not reference the original dataframe in the other function, i.e. print_df_in_a_function(). Could anyone please advise the mistake I made here?
It displays None upon calling ...
0
votes
1
answer
705
views
Why doesn't assignment of new values change the original dict? [duplicate]
d = {'a':'1', 'b':'2', 'c':'3'}
def change_d(d):
dd = {}
d = dd
print(d)
change_d(d)
print(d)
I suppose both print() should print out an empty dict, but they are different:
{}
{'a': '1',...
marlon's user avatar
- 7,929
0
votes
3
answers
88
views
How to access global variable [duplicate]
From the following code, I wish for the output of names to contain an array of names taken from a column in a csv file. When the function is run, the array remains empty. Why?
def cp_identifier():
...
user avatar
user10003651
-1
votes
1
answer
96
views
My variables updated in a function don't work outside the function [duplicate]
hi = 1
result = 0
def hello(x):
x = x + 10
result = x
helli(hi)
print(result)
Why does my code Output "0" and not 11?
1
vote
1
answer
106
views
Code not working how I expected (Python 3.x.x) [duplicate]
I've recently being self teaching python and I'm making my first attempt at a game. The idea is the user must guess what the answer is (integer) My code is not yet complete but the first half won't ...
-4
votes
2
answers
86
views
Scope in Python 3.6.And the Error [duplicate]
How does the code below give the output as None?
def funct(x):
x=7
x=11
x=test(x)
print(x)
Here is another code snippet:-
def func():
print (x)
x = 90
x = 1
func()
The output for this ...
-1
votes
2
answers
64
views
Tkinter python3 [duplicate]
I have been trying to use the tkinter module for creating a user interface for a project I've been working on. For some reason, the command doesn't do what i'm asking from it and the price stays as 0 ...
-1
votes
1
answer
51
views
function without args that uses elements precreated [duplicate]
I'm making a program that uses a variable called "lev" that must be used in all my functions, but it doesn't need to be an argument of the function. I mean, the function makes some things and then ...
0
votes
0
answers
27
views
why my program rewrite the same result in calculator [duplicate]
am trying to make a simple calculator using python but for some reason he keep giving me the same result everytime
here is the code
def inputing(num2, op) :
op = input("operator")
...
0
votes
0
answers
28
views
Alternative for changing a variable from outer scope in a function [duplicate]
I am making a basic tic tac toe game as this is the first time I am coding but I am running into an issue where I define a variable outside the function and try to change it inside but I am unable to ...
0
votes
0
answers
26
views
I am changing the value of a global variable inside a function, but as soon as I get out of that function it is changing back to its previous value [duplicate]
I am changing the value of a global variable inside a function, but as soon as I get out of that function it is changing back to its previous value.
I have created a game in which a rabbit(represented ...
62
votes
12
answers
60k
views
Do you use the "global" statement in Python? [closed]
I was reading a question about the Python global statement ( "Python scope" ) and I was remembering about how often I used this statement when I was a Python beginner (I used global a lot) and how, ...