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
CodePudding user response:
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...
CodePudding user response:
Maybe just this :
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
CodePudding user response:
I tried this code and it works... Looks like you were checking all the options...
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")
CodePudding user response:
Looks like you need 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")
You don't need to loop if you have the key