Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting multiple outputs from my dictionary when I need only one

Im having a little problem with only printing the corresponding abbreviation that is stored in a dictionary

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

This is my output

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

I only need the entered abbreviation key value not all the Abbreviation not found outputs, I hope this makes sense

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The good point of a dictionary is that is "indexed" so you don’t need to find the key. You are following a array approach, and that is a wrong approach.

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

You can find more information about dictionaries here, here and on google. It is very important to know when to use arrays, dictionaries, lists, queues…

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading