如何在Python中使用input()指定字典
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)
当我运行它时,有:
“对于 dict1.items() 中的 i、j: AttributeError:'str'对象没有属性'items'“
是否可以使用输入功能或者我需要什么?
Python - I'm beginner. I want to use input() to call one of possible dictionaries (in my example there are: NN and NN1) and then run functions on that chosen dictionary.
Here is part of my code (I need this "tik = i" later, but now it does not matter):
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)
When I run it, there is:
"for i, j in dict1.items():
AttributeError: 'str' object has no attribute 'items' "
Is it possible to use input function or do I need sth?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在代码中进行以下更改:
you need these changes in your code: