Access assignment through concatenation? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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!

25th Mar 2018, 9:47 PM
Matistjati XVII
Matistjati XVII - avatar
2 Answers
+ 2
another way would be using eval but beware of the huge security hole it can be without prior input validation: print (eval(varchosen))
26th Mar 2018, 5:10 AM
Amaras A
Amaras A - avatar
+ 3
You could use an if statement. if input==1: print(var1) else: print(var2)
25th Mar 2018, 10:24 PM
Ariela
Ariela - avatar