我们可以让python从raw_input()内置函数中获取变量而不是字符串吗?
我想知道是否还有其他方法可以让python我们可以让python从raw_input()内置函数中获取变量而不是字符串吗?,python,Python,我想知道是否还有其他方法可以让pythonraw\u input()为我获取变量调用 我的理解是,raw_input()接受其中写入的任何注释并将其转换为字符串,就像%r一样,它只是以调试模式字符串格式打印所有内容 但如果我们试图转换如下内容: test1 = int(raw_input("Please write some number:> ")) print test1 * 100 如果我们输入100,下一行将在其上执行一个计算函数,这是我非常感兴趣的实现,但以这种方式: 这不是一
raw\u input()
为我获取变量调用
我的理解是,raw_input()
接受其中写入的任何注释并将其转换为字符串,就像%r
一样,它只是以调试模式字符串格式打印所有内容
但如果我们试图转换如下内容:
test1 = int(raw_input("Please write some number:> "))
print test1 * 100
如果我们输入100,下一行将在其上执行一个计算函数,这是我非常感兴趣的实现,但以这种方式:
这不是一个错误的尝试,而是给出我想要什么的大致想法
如果这是python获取输入的方式,例如,当输入被指定为a
时,我们将打印a
,以此类推,使用b
和c
等
python中有没有一种方法可以让程序以变量而不是字符串的形式从我们这里获取输入。有转换吗
提前感谢大家的帮助 否,正如您所说的
raw\u input
返回一个字符串。如果要根据用户输入返回相关结果,最好的方法是使用字典将变量名映射到它们的值:
my_dict = {'a': "I will like some candies"
'b': "I will like some chocolates"
'c': "I will rather die but have fried chicken" }
test2 = raw_input("Please write the variable name you want to print"
warning = "Please choose one of the aforementioned choices (a, b, c)"
print my_dict.get(test2,warning)
如果是全局变量,则可以执行以下操作:
test1 = int(raw_input("Please write some number:> "))
print test1 * 100
a = "I will like some candies"
b = "I will like some chocolates"
c = "I will rather die but have fried chicken"
test2 = raw_input("Please write the variable name you want to print ")
print globals()[test2]
如果没有,则必须指定变量的名称空间
test1 = int(raw_input("Please write some number:> "))
print test1 * 100
a = "I will like some candies"
b = "I will like some chocolates"
c = "I will rather die but have fried chicken"
test2 = raw_input("Please write the variable name you want to print ")
print globals()[test2]