how can I get my output to be the string of a name variable, if the input is equal to the name variable, I want the string of the variable to print. Thanks in advance.
a = 'My name'
b = 2
c = 3
print('input any alphabet of your choice below:')
print('Enter your name below')
print('input below')
running = True
while running:
if user_input == a:
print('This is:', a)
running = False
else:
print('wrong input')
>Solution :
Use a dictionary instead of named variables, so that you can use the input as a key in the dictionary:
my_vars = {
'a': 'My name',
'b': 2,
'c': 3,
}
while True:
try:
print(f"This is: {my_vars[input()]}")
break
except KeyError:
print('wrong input')
x
wrong input
b
This is: 2