+ 1
Access assignment through concatenation?
I've been trying to access an assignment by concatenating input. Example: var1 = 10 var2 = 20 varinput = input("var 1 or 2?") varchosen = "var" + varinput print (varchosen) Now, what i want it to do is print 10 if input is 1, 20 if input is 2. Keep in mind, this is just an example to showcase an idea. Is this simply impossible due to the way python works? Thanks in advance!
2 Respuestas
+ 2
another way would be using eval but beware of the huge security hole it can be without prior input validation:
print (eval(varchosen))
+ 3
You could use an if statement.
if input==1:
print(var1)
else:
print(var2)