【问题标题】:Getting multiple outputs from my dictionary when I need only one当我只需要一个时从我的字典中获取多个输出
【发布时间】:2022-01-15 02:08:53
【问题描述】:

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

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") 

这是我的输出

Enter a Abbreviation: UI
Abbreviation not found
Abbreviation not found
Abbreviation not found
UI: User Interface
Abbreviation not found
Abbreviation not found 

我只需要输入的缩写键值而不是所有的 Abbreviation not found 输出,我希望这是有道理的

【问题讨论】:

  • 什么你不只是删除elif 部分?也许您也可以尝试输入您想要的输出。

标签: python dictionary for-loop


【解决方案1】:

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

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 上找到有关字典 herehere 的更多信息。知道何时使用数组、字典、列表、队列非常重要……

【讨论】:

    【解决方案2】:

    也许就是这样:

    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
    

    【讨论】:

      【解决方案3】:

      我尝试了这段代码,它可以工作...看起来您正在检查所有选项...

      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")
      

      【讨论】:

        【解决方案4】:

        看来你需要dict.get

        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"
                      }
        
        result = dictionary.get(user.upper())
        if result:
            print(user + ":" + " " + result )
        else:
            print("Abbreviation not found") 
        

        如果您有key,则无需循环

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-09-05
          • 1970-01-01
          • 1970-01-01
          • 2014-10-02
          • 1970-01-01
          • 2022-01-20
          • 2023-04-02
          相关资源
          最近更新 更多