1

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

4 Answers4

2

I solved it in the following way:

dict = {
  "a": 20,
  "b": 3,
  "c": 32,
  "d": 63
}
name = input("Name:")
print(dict[name])
2
>>> x = 1
>>> 
>>> s = 'x'
>>> s
'x'
>>> eval(s)
1

use eval function:

a = 20
name = input("Variable name:") 
print(eval(name))

result:

20
islam abdelmoumen
  • 662
  • 1
  • 3
  • 9
2

user_value = { "a": 20, "b": 3, "c": 32, "d": 63 }

name_value = input(" Name : ")

print("Your Entered value is : "dict[name])

Hasan Raza
  • 424
  • 3
  • 9
1

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