如何使用 input() 在 Python 中指定字典
How to use input() to specify a dictionary in Python
Python - 我是初学者。我想使用 input() 调用其中一个可能的词典(在我的示例中有:NN 和 NN1),然后在该所选词典上调用 运行 函数。
这是我的部分代码(我稍后需要这个“tik = i”,但现在没关系):
NN = {"short name1": "full name1", "short name2": "full name2", "short name3": "full name3"}
NN2 = {"short name4": "full name4", "short name5": "full name5", "short name6": "full name6"}
dict1 = input ("your choice: NN / NN1? ")
for i, j in dict1.items():
tik = i
print(j)
当我运行它时,有:
"for i, j in dict1.items():
AttributeError: 'str' object has no attribute 'items' "
是否可以使用输入功能或者我需要什么?
您需要对代码进行以下更改:
inp_dict = {'NN':NN , 'NN1':NN2}
dict1 = input ("your choice: NN / NN1? ")
for i, j in inp_dict[dict1].items():
tik = i
print(j)
Python - 我是初学者。我想使用 input() 调用其中一个可能的词典(在我的示例中有:NN 和 NN1),然后在该所选词典上调用 运行 函数。 这是我的部分代码(我稍后需要这个“tik = i”,但现在没关系):
NN = {"short name1": "full name1", "short name2": "full name2", "short name3": "full name3"}
NN2 = {"short name4": "full name4", "short name5": "full name5", "short name6": "full name6"}
dict1 = input ("your choice: NN / NN1? ")
for i, j in dict1.items():
tik = i
print(j)
当我运行它时,有:
"for i, j in dict1.items(): AttributeError: 'str' object has no attribute 'items' "
是否可以使用输入功能或者我需要什么?
您需要对代码进行以下更改:
inp_dict = {'NN':NN , 'NN1':NN2}
dict1 = input ("your choice: NN / NN1? ")
for i, j in inp_dict[dict1].items():
tik = i
print(j)