如何在Python中使用input()指定字典

发布于 2025-01-14 06:27:07 字数 564 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心清如水 2025-01-21 06:27:07

您需要在代码中进行以下更改:

inp_dict = {'NN':NN , 'NN1':NN2}
dict1 = input ("your choice: NN / NN1? ")
for i, j in inp_dict[dict1].items():
    tik = i
    print(j)

you need these changes in your code:

inp_dict = {'NN':NN , 'NN1':NN2}
dict1 = input ("your choice: NN / NN1? ")
for i, j in inp_dict[dict1].items():
    tik = i
    print(j)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文