The user enters a variable name into the input form and I want to see its value
a = 20
b = 3
c = 32
d = 63
name = input("Variable name:") #FOR EXAMPLE "b"
print(name) #PRINT 3
The user enters a variable name into the input form and I want to see its value
a = 20
b = 3
c = 32
d = 63
name = input("Variable name:") #FOR EXAMPLE "b"
print(name) #PRINT 3
I solved it in the following way:
dict = {
"a": 20,
"b": 3,
"c": 32,
"d": 63
}
name = input("Name:")
print(dict[name])
>>> x = 1
>>>
>>> s = 'x'
>>> s
'x'
>>> eval(s)
1
use eval function:
a = 20
name = input("Variable name:")
print(eval(name))
result:
20
user_value = { "a": 20, "b": 3, "c": 32, "d": 63 }
name_value = input(" Name : ")
print("Your Entered value is : "dict[name])
using globals()
a = 20
b = 3
c = 32
d = 63
name = input("Variable name:") #FOR EXAMPLE "b"
print(globals()[name])
output
Variable name:b
3