Python 将用户输入更改为列表的名称
我正在用python编写一个程序。为了提高程序的效率,我希望有一种方法可以接受用户输入并将其更改为列表的名称。基本上,就像去掉上面的引号 像这样Python 将用户输入更改为列表的名称,python,list,input,Python,List,Input,我正在用python编写一个程序。为了提高程序的效率,我希望有一种方法可以接受用户输入并将其更改为列表的名称。基本上,就像去掉上面的引号 像这样input=input(“你叫什么名字”) 假设你的名字是特德。 我有一个列表是Ted=[“诸如此类] 所以基本上我想把输入转换成Ted,就是Ted lists = { "Ted": ["Blah", "Blahh"], "Joe": ["Foob", "zerx"], } name = input("What is your name?
input=input(“你叫什么名字”)
假设你的名字是特德。
我有一个列表是Ted=[“诸如此类]
所以基本上我想把输入转换成Ted,就是Ted
lists = {
"Ted": ["Blah", "Blahh"],
"Joe": ["Foob", "zerx"],
}
name = input("What is your name? ")
if name not in lists:
print("Don't have a list for that name")
else:
print("The list is %s" % (lists[name],))
如果您使用的是Python2.x,请使用raw_input
而不是input
如果您使用的是Python2.x,请使用raw_input
而不是input
如果您使用的是Python2.x,请使用raw_input
而不是input
如果您使用的是Python 2.x,请使用
raw\u input
而不是input
。您可以试试字典:
dict_1 = {}
name = input("What is your name")
dict_1[name] = ["blah", "blah"]
阅读有关Internet上字典的更多信息。您不应该使用
input
作为变量名称,因为它是Python保留的关键字。您可以尝试使用字典:
dict_1 = {}
name = input("What is your name")
dict_1[name] = ["blah", "blah"]
阅读有关Internet上字典的更多信息。您不应该使用
input
作为变量名称,因为它是Python保留的关键字。您可以尝试使用字典:
dict_1 = {}
name = input("What is your name")
dict_1[name] = ["blah", "blah"]
阅读有关Internet上字典的更多信息。您不应该使用
input
作为变量名称,因为它是Python保留的关键字。您可以尝试使用字典:
dict_1 = {}
name = input("What is your name")
dict_1[name] = ["blah", "blah"]
阅读有关Internet上词典的更多信息。您不应该使用
input
作为变量名称,因为它是Python保留的关键字。我相信这是您想要的:
globals()['ted'] = 42
print(ted)
我相信这就是你想要的:
globals()['ted'] = 42
print(ted)
我相信这就是你想要的:
globals()['ted'] = 42
print(ted)
我相信这就是你想要的:
globals()['ted'] = 42
print(ted)
如果必须,为什么不使用字典?{'Ted':[Blah,Blahh]}我想Python版本是3.x?是的,使用字典。如果必须,为什么不使用字典?{'Ted':[Blah,Blahh]}我想Python版本是3.x?是的,使用字典。如果必须,为什么不使用字典?{'Ted':[Blah,Blahh]}我想Python版本是3.x?是的,使用字典。如果必须,为什么不使用字典?{'Ted':[Blah,Blahh]}我想Python版本是3.x?是的,使用字典。