首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我只需要一个时,从我的字典中获得多个输出

当我只需要一个时,从我的字典中获得多个输出
EN

Stack Overflow用户
提问于 2021-12-10 11:05:11
回答 4查看 40关注 0票数 0

我只打印存储在字典中的相应缩写有一点问题

代码语言:javascript
复制
user = input("Enter a Abbreviation: ")

dictionary = {"ADSL": "Application Programming Interface",
              "IDE": "Integrated Development Enviroment",
              "SDK": "Software Development Kit",
              "UI": "User Interface",
              "UX": "User eXperience",
              "OPP": "Object Oriented Programming"
              }

for x in dictionary:
    if user in x:
        print(user + ":" + " " + dictionary[x])

    elif x != dictionary:
        print("Abbreviation not found") 

这是我的输出

代码语言:javascript
复制
Enter a Abbreviation: UI
Abbreviation not found
Abbreviation not found
Abbreviation not found
UI: User Interface
Abbreviation not found
Abbreviation not found 

我只需要输入缩写键值,不需要所有缩写没有找到输出,我希望这是有意义的。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2021-12-10 11:10:56

字典的优点是它是“索引的”,所以你不需要找到关键。您遵循的是数组方法,这是一个错误的方法。

代码语言:javascript
复制
user = input("Enter a Abbreviation: ")

dictionary = {"ADSL": "Application Programming Interface",
              "IDE": "Integrated Development Enviroment",
              "SDK": "Software Development Kit",
              "UI": "User Interface",
              "UX": "User eXperience",
              "OPP": "Object Oriented Programming"
              }

if user in dictionary:
   print(user + ":" + " " + dictionary[user])
else:
   print("Abbreviation not found")

您可以找到更多关于字典这里这里和google的信息。知道何时使用数组、字典、列表、队列非常重要.

票数 2
EN

Stack Overflow用户

发布于 2021-12-10 11:12:19

也许只是这个:

代码语言:javascript
复制
user = input("Enter a Abbreviation: ")

dictionary = {"ADSL": "Application Programming Interface",
              "IDE": "Integrated Development Enviroment",
              "SDK": "Software Development Kit",
              "UI": "User Interface",
              "UX": "User eXperience",
              "OPP": "Object Oriented Programming"
              }

for x in dictionary:
    if user in x:
        print(user + ":" + " " + dictionary[x])
    else:
        continue
票数 1
EN

Stack Overflow用户

发布于 2021-12-10 11:15:40

我试过这个密码,它有效..。看来你在检查所有的选项..。

代码语言:javascript
复制
user = input("Enter a Abbreviation: ")

dictionary = {"ADSL": "Application Programming Interface",
              "IDE": "Integrated Development Enviroment",
              "SDK": "Software Development Kit",
              "UI": "User Interface",
              "UX": "User eXperience",
              "OPP": "Object Oriented Programming"
              }

found = 0
for x in dictionary:
    if user in x:
        found = 1
        print(user + ":" + " " + dictionary[x])

if found == 0:
    print("Abbreviation not found")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70303554

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档