Last active
February 15, 2019 23:31
-
-
Save peta909/b58a671896b3fa31b02d0f70eb313701 to your computer and use it in GitHub Desktop.
Using Python Dict and accessing key and value using indexes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
food_cook_dict = { | |
"pasta" : "Aaron", | |
"bread" : "Thomas", | |
"rice" : "John", | |
"Soup" : "Kate", | |
"noodles" : "Liam", | |
} | |
# dictionary are unordered unlike lists till python 3.6 | |
print ("The following is the menu of the day and their cooks") | |
for i, x in enumerate(food_cook_dict): | |
print (str(i) + ". " + str(x)) | |
#no idea how to start the item from index 1 instead of 0. | |
choice = raw_input("Please Enter selection number: ")#choice is of type(str) | |
print ("Food choice: " + food_cook_dict.keys()[int(choice)])#access to the key based on index(choice) | |
print ("Cook assigned: " + food_cook_dict.values()[int(choice)])#access to value based on index(choice) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if key is used to select value instead of index:
cook_assigned = food_cook_dict.get(choice, "Invalid Entry")