获取变量的字符串作为输出
getting string of a variable as an output
如何让我的输出成为一个名称变量的字符串,如果输入等于名称变量,我想要打印变量的字符串。提前致谢。
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')
使用字典而不是命名变量,这样您就可以将输入用作字典中的键:
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
如何让我的输出成为一个名称变量的字符串,如果输入等于名称变量,我想要打印变量的字符串。提前致谢。
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')
使用字典而不是命名变量,这样您就可以将输入用作字典中的键:
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