This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 2 years ago.
I am currently trying to define a number of variables as empty lists (equal in quantity to the number of iterations of the loop). So far I've tried the following:
for i in [1,2,3]:
locals()["temp_set_" + str(i)] = []
When I try to print(temp_set_1) outside of the loop in order to verify the output, I get the following error: NameError: global name 'temp_set_1' is not defined
I am not sure what I'm doing wrong at the moment and would appreciate it if someone could point me in the right direction.
In my mac, with python3.6, it works.
for i in [1,2,3]:
locals()["temp_set_" + str(i)] = []
Related
This question already has answers here:
What is the purpose of the single underscore "_" variable in Python?
(5 answers)
How can I get around declaring an unused variable in a for loop?
(10 answers)
Closed 1 year ago.
I'm looking at some tensorflow stuff and I understand for loops or atleast I think I do, however I came across for _ in range(20) and was wondering what is the meaning of the _ in this case. I am used to for x in range or for i in range stuff and understand those but haven't been able to understand what i've read on the underscore
When you are not interested in some values returned by a function we use underscore in place of variable name . Basically it means you are not interested in how many times the loop is run till now just that it should run some specific number of times overall.
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 4 years ago.
I am trying to use a Python list comprehension to get the Variable Name SW1 to equal the string "sw1" but I keep getting an error saying sw1 not defined
VarList = ["SW1"]
VarListEnd = ["sw1"]
list3 = [exec("%s="%x + "%s"%y) for x in VarList for y in VarListEnd]
list3
How do I amend the exec statement because that is where I think the error is?
Really appreciate your help. Thanks in advance.
You don't need exec for that. To create variables, you can do
for name, value in zip(VarList, VarListEnd):
locals()[name] = value
This question already has answers here:
How do you create different variable names while in a loop? [duplicate]
(9 answers)
How do I create variable variables?
(17 answers)
Closed 4 years ago.
As of right now i am a beginner with python. I wanna make a giant list of variables without having to write out each one, so far heres the idea:
n=0
i=None
for i in range(10):
n = n + 1
x="hi"
var = x+"{0}".format(n)
print(var)
the output are strings but i want to make them variable that i can define freely. Any pointers>
This question already has answers here:
How do I get a result (output) from a function? How can I use the result later?
(4 answers)
Closed 6 years ago.
Sorry for the title but I'm not sure how else to word it. Anyway I'm just starting to learn Python and ran into this problem. I'm trying to assign a variable to a function call, where the function contains input()
Unfortunately this never assigns anything to the variable a
def question(letter):
input(letter + '? ')
a = question('a')
print(a)
So I guess my real question is, why doesn't that work? Why doesn't it assign the user input to the variable a?
Thanks
You need to return the user input. Right now the user input is captured nowhere.
def question(letter):
return input(letter + '? ')
a = question('a')
print(a)
This question already has answers here:
Python function global variables? [duplicate]
(6 answers)
Closed 7 years ago.
I am trying to set a dictionary for the staff's salary in Python. I want to create two functions, getname() and getsalary() so I tried:
empDic={}
def setname(n):
empDic={'n':''}
And then in python interactive 2.7 I typed:
>>>setname('marson')
>>>print empDic
{}
The result is still an empty dictionary, How can I deal with that?
You would need to modify the original dict:
empDic={}
def setname(d, n):
d[n] = ""
setname(empDic,"marson")
print(empDic)
{'marson': ''}
You are creating a new local variable empDic in your function, you are also using a string "n" not the name n passed in to the function.