Skip to content

Instantly share code, notes, and snippets.

@ploorp
Last active January 4, 2022 19:57
Show Gist options
  • Save ploorp/01b22388ddac8ac173baf46dad2ef38f to your computer and use it in GitHub Desktop.
Save ploorp/01b22388ddac8ac173baf46dad2ef38f to your computer and use it in GitHub Desktop.
Unit 8 - Using Dictionaries - Lessons 4 - 10 Assignment
4, Try ? Except to Get a Key
caffeine_level = {"espresso": 64, "chai": 40, "decaf": 0, "drip": 120, "matcha":30}
try:
print(caffeine_level["matcha"])
except:
print("Unknown Caffeine Level")
5. Safely Get a Key
user_ids = {"teraCoder": 100019, "pythonGuy": 182921, "samTheJavaMaam": 123112, "lyleLoop": 102931, "keysmithKeith": 129384}
tc_id = user_ids.get("teraCoder", 100000)
print(tc_id)
stack_id = user_ids.get("superStackSmash", 100000)
print(stack_id)
6. Delete a Key
available_items = {"health potion": 10, "cake of the cure": 5, "green elixir": 20, "strength sandwich": 25, "stamina grains": 15, "power stew": 30}
health_points = 20
health_points += available_items.pop("stamina grains", 0)
health_points += available_items.pop("power stew", 0)
health_points += available_items.pop("mystic bread", 0)
print(available_items, health_points)
7. Get All Keys
user_ids = {"teraCoder": 100019, "pythonGuy": 182921, "samTheJavaMaam": 123112, "lyleLoop": 102931, "keysmithKeith": 129384}
num_exercises = {"functions": 10, "syntax": 13, "control flow": 15, "loops": 22, "lists": 19, "classes": 18, "dictionaries": 18}
users = user_ids.keys()
lessons = num_exercises.keys()
print(users, lessons)
8. Get All Values
num_exercises = {"functions": 10, "syntax": 13, "control flow": 15, "loops": 22, "lists": 19, "classes": 18, "dictionaries": 18}
total_exercises = 0
for i in num_exercises.values():
total_exercises += i
print(total_exercises)
9. Get All Items
pct_women_in_occupation = {"CEO": 28, "Engineering Manager": 9, "Pharmacist": 58, "Physician": 40, "Lawyer": 37, "Aerospace Engineer": 9}
for x, y in pct_women_in_occupation.items():
print('Women make up ' + str(y) + ' percent of ' + x + 's.')
10. Review
tarot = { 1: "The Magician", 2: "The High Priestess", 3: "The Empress", 4: "The Emperor", 5: "The Hierophant", 6: "The Lovers", 7: "The Chariot", 8: "Strength", 9: "The Hermit", 10: "Wheel of Fortune", 11: "Justice", 12: "The Hanged Man", 13: "Death", 14: "Temperance", 15: "The Devil", 16: "The Tower", 17: "The Star", 18: "The Moon", 19: "The Sun", 20: "Judgement", 21: "The World", 22: "The Fool"}
spread = {}
spread["past"] = tarot.pop(13)
spread["present"] = tarot.pop(22)
spread["future"] = tarot.pop(10)
for x, y in spread.items():
print('Your ' + x + ' is the ' + y + ' card.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment