Python Forum
Accessing a dictionary - KeyError ??
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accessing a dictionary - KeyError ??
#1
I am trying to practice lists and dictionaries. I know this is not the ideal way to code this specific task but I wanted to use lists and dictionaries to make sure I knew how to use them and access the items inside them. When I tried to run the following code and give the raw input 'Shake' or anything that will yield the protein shake part of the function, I get a KeyError 'P', but I don't have a Key 'P' in the dictionary that I am requesting from. It is referring to the line in the function cook_book that reads: print "Ingredients: " + str(ingredients_and_recipes['Protein Shake'[0]]). I might be confused as to how to explicitly write a request from a dictionary. Please help! Code:

Cheese = ['American', 'Swiss', 'Parmesan','Pepper Jack', 'Chedder', 'Mozzarella', 'Provalone']
dairy = ['Milk', 'Butter', Cheese, 'Egg', 'Yogurt', 'Ice Cream', 'Cream', 'Cottage Cheese', 'Cream Cheese', 'Gelato', 'Sour Cream', 'Whipped Cream']

Chicken = ['Breast', 'Wings', 'Drumbstick', 'Thighs']
Beef = ['Steak', 'Ribs', 'Ground Beef', 'Meat Balls']
Pork = ['Porkchops', 'Pulled Pork', 'Bacon', 'Ham']
meat = [Chicken, Beef, Pork, 'Turkey', 'Salami', 'Sausage', 'Duck']

vegetables = ['Broccoli', 'Tomato', 'Spinach', 'Cabbage', 'Onion', 'Celery', 'Cucumber', 'Lettuce', 'Carrot', 'Scallion', 'Basil', 'Pepper']

fruits = ['Apple', 'Orange', 'Banana', 'Mango', 'Avocado', 'Blueberry', 'Coconut', 'Grape', 'Lemon', 'Lime', 'Pineapple']

nuts = ['Almond', 'Cashew', 'Walnut', 'Hazelnut', 'Pistachio', 'Pecan', 'Peanut']

spices_and_dressings = ['Red Sauce', 'Peanut Butter', 'Jelly', 'Nutmeg', 'Cajun', 'Pepper Flakes', 'Ketchup', 'Aioli', 'Rosemary', 'Salt', 'Pepper', 'Cayenne', 'Honey']

carbs = ['Rice', 'Bread', 'Pasta', 'Potato']

cooking_items = ['Bread Crumbs', 'Flour', 'Sugar', 'Ice', 'Protein Powder']


#Definitions
def cook_book():
  meal = raw_input("What meal would you like to cook?")
  if meal in ('Chicken Parmesan', 'Chicken Parm', 'chicken parm', 'chicken parmesan'): 
    print "Ingredients: " + str(ingredients_and_recipes['Chicken Parmesan'][0])
    print "Instructions: " + str(ingredients_and_recipes['Chicken Parmesan'][1]) 
  elif meal in ('Crepes', 'Crepe', 'crepes', 'crepe'):
    print "Ingredients: " + str(ingredients_and_recipes['Crepes'][0])
    print "Instructions: " + str(ingredients_and_recipes['Crepes'][1])
  elif meal in ('Protein Shake', 'protein shake', 'Protein shake', 'Shake', 'shake', 'protein Shake'):
    print "Ingredients: " + str(ingredients_and_recipes['Protein Shake'[0]])
    print "Instructions: " + str(ingredients_and_recipes['Protein Shake'[1]])
  else:
    print "Sorry. That is not in Eoin's Cook Book" 



#Declarations----Ingredients and Instructions
############# CHICKEN PARMESAN ###########
chicken_parmesan_ingredients = [meat[0][0], spices_and_dressings[8], spices_and_dressings[9], cooking_items[0], dairy[3], dairy[1], spices_and_dressings[0], dairy[2][5], dairy[2][2]]

chicken_parmesan = {
  'step_1_chicken_parmesan' : "Pulverize Chicken. Prepare bowl of beaten egg and bowl of bread crumbs", 'step_2_chicken_parmesan' : "Salt and Pepper. Then dip breast in egg, then bread crumbs", 'step_3_chicken_parmesan' : "Fry in butter in hot skillet, turning and browning for 10 minutes or until chicken is done.", 'step_4_chicken_parmesan' : "Remove chicken from skillet, and replace with Red Sauce, heat thoroughly, and then add chicken.", 'step_5_chicken_parmesan' : "Place slices of mozzarella cheese on chicken, sprinkle with parmesan, and cook until cheese is slightly melted. Serve with pasta:)"
  }

chicken_parmesan_instructions = [
  chicken_parmesan['step_1_chicken_parmesan'], chicken_parmesan['step_2_chicken_parmesan'], chicken_parmesan['step_3_chicken_parmesan'], chicken_parmesan['step_4_chicken_parmesan'], chicken_parmesan['step_5_chicken_parmesan']
]
############################################
################# CREPES ###################
crepes_ingredients = [dairy[3], dairy[0], cooking_items[1], cooking_items[2], dairy[1]]

crepes = {
  'step_1_crepes' : 'Prepare in a bowl the following: beaten eggs, milk, flour, sugar, and melted butter. Mix thoroughly, but keep a sort of thick consistency.', 'step_2_crepes' : 'Melt butter onto skillet to ensure non-stick, fill skillet with thin layer of batter. (Quick cook!)' , 'step_3_crepes' : 'Flip only once to cook both sides. Serve with nutella, whipped cream, and fruit'
}

crepes_instructions = [
  crepes['step_1_crepes'], crepes['step_2_crepes'], crepes['step_3_crepes']
]
#############################################
################# Protein Shake #############
protein_shake_ingredients = [cooking_items[3], dairy[0], dairy[4], 'Frozen' + fruits[2], spices_and_dressings[1], cooking_items[4], spices_and_dressings[3], spices_and_dressings[12]]

protein_shake = {
  'step_1_protein_shake' : 'Simply add the ingredients into a blender in the order they are presented. Then just hit a bunch of settings on the blender to make the shake the desired consistency. Feel free to include more fruits or spices. frozen fruits will make the consistency thicker and, in my opinion, more delicious! Enjoy your GAINZZZZ.'
}

protein_shake_instructions = [
  protein_shake['step_1_protein_shake']
]

#############################################
ingredients_and_recipes = {'Chicken Parmesan' : [chicken_parmesan_ingredients, chicken_parmesan_instructions], 'Crepes' : [crepes_ingredients, crepes_instructions], 'Protein Shake' : [protein_shake_ingredients, protein_shake_instructions]}


#Main

cook_book()
Reply
#2
Why do you put an index next to the dictionary key? See what you get from it?
>>> 'Protein Shake'[0]
'P'
That 'P' is the key but obviously, there is no such a key.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Issue accessing data from Dictionary/List in the right format LuisSatch 2 2,203 Jul-25-2020, 06:12 AM
Last Post: LuisSatch
  accessing Json dictionary lshankar 1 1,839 Dec-16-2019, 06:18 AM
Last Post: ndc85430
  why is this dictionary giving a Keyerror? philipbergwerf 1 1,625 Dec-11-2019, 08:03 PM
Last Post: DreamingInsanity
  Accessing nested dictionary values. Plistlib, Python 2.7 williamlombard 32 20,777 Sep-29-2017, 06:46 AM
Last Post: williamlombard

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020