In our first lesson, you caught some Marvel Rivals heroes in dictionaries. Today, we’re training Pokémon! Dictionaries are like Pokédex entries—you can add new stats, tweak their moves, and even forget old tricks. Let’s level up those key-value pairs with some Pokémon flair!
We need to start this lesson by defining a very basic dictionary... PIKACHU!!!
pikachu = {'name': 'Pikachu', 'type': 'Electric'}What If you have a dictionary defined where you want to add new key-value pairs to it?
Let's add a LEVEL and HP to our Pikachu
#Print the dictionary:
print(pikachu)
#Add a new key-value pair
pikachu['hit_points'] = 10
pikachu['level'] = 1
#Print the UPDATED dictionary:
print(pikachu)
For this exercise, we will create a BRAND NEW DICTIONARY
#Start with an EMPTY Dictionary:
pokemon = { }
Add the following KEY-VALUES to your card
Name
HP
Attack
Defense
#Add a new key-value pair
pokemon ['Name'] = ''
pokemon ['HP'] = ''
pokemon ['Attack'] = ''
pokemon ['Defense'] = ''
#Print The Card
print(pokemon)
#Print The Dictionary:
print(person)
#Let's Modify this info:
pokemon ['Name'] = 'Mike Wazowski'
pokemon ['HP'] = '10'
pokemon ['Attack'] = '1'
pokemon ['Defense'] = '5'
print(pokemon)
Yes. There is a method called update:
Using update, change the value of Attack and Defense
pokemon.update({'Attack': 30})
pokemon.update({'Defense': 50})
print(pokemon)
If you provide a key which doesn't exist, update will ADD THE NEW KEY!!! This doesn't give you an error!!
You’re a Pokémon card designer crafting a new card for the trading card game! Build a dictionary for a Pokémon card by collecting input from the user for each stat. Use predefined lists to offer realistic options for some stats. Store all keys in a list so you can show them without fancy dictionary tricks. Then, give the user a chance to update the card with a new stat or tweak an old one.
Create a Pokémon card dictionary with these 5 key-value pairs (all single values):
'name': String (user types any Pokémon name, e.g., "Pikachu")
'type': String (choose from a list: "Fire", "Water", "Grass", "Electric")
'hp': Integer (choose from a list: 50, 60, 70, 80, 90)
'attack': Random integer between 20 and 40
'weakness': String (choose from a list: "Fire", "Water", "Grass", "Electric")
You MUST store all keys the user creates in a LIST of keys (that you can use later in this program)
Once the card is created, ask the user to add a new stat
Allow the user to update any stat
Show them the possible stats to update from the list of keys you saved
Use try/except to catch error such as KeyError or ValueError, etc...
Why Would We Delete a Key?
Sometimes, a Pokémon’s Pokédex entry—or card—has stuff that’s just not worth keeping! Maybe a stat’s outdated, useless, or doesn’t fit the vibe anymore. Deleting a key with del lets you clean up your dictionary like a pro trainer trimming the fat off their team. It’s like telling Pikachu, “Forget that weak move—you’re better than that now!”
This is also useful if you mistakenly add a key!! (Remember update?? What if it went rogue!!)
Remember... This can throw a KeyError!!!
del dictionary_name['key']
When to Use: You’re done with a stat and don’t need it back—like forgetting a weak move!
We can use POP to both get rid of a key, but return the VALUE to us before we ditch it!
We also give it a DEFAULT VALUE just in case the key doesn't exist. If we don't it can throw a KeyError
dictionary_name.pop('key', default)
When to Use: You want to snag the value before tossing it—like trading away a stat for a new card!
Trainers, your Pokémon card’s looking slick—but is it too perfect? Time to shake things up in the Pokémon Card Lab! You’ve already brewed a killer card with your mad skills, but now it’s time to prune the weak spots. Zap stats into oblivion with del, or yoink ‘em out with pop() like a sneaky Team Rocket heist! Modify your code, dodge those pesky errors, and see if your card survives the chaos—or ends up a laughingstock in Professor Oak’s reject pile. Let’s get destructive!
Goal:
Extend your Pokémon Card Creator to delete stats with del and pop(), keeping your card_keys list in sync.
Assumption:
You’ve already coded the Pokémon Card Creator with:
Stat lists (types, hp_options, etc.).
A card dictionary built from user input (name, type, hp, attack, weakness).
A card_keys list tracking keys.
A basic update option (Option 1).
Take your existing Pokémon Card Creator code and level it up! You’ve got a shiny Pokémon card, but now it’s time to trim or trade stats. Keep your stat lists and card creation code as-is, then tweak the menu to add deletion options.
Here’s how:
Keep Your Setup: Use your existing lists (types, hp_options, attack_options, weaknesses) and the code that builds card and card_keys with user input.
Expand the Menu: After printing the initial card and card_keys, update your menu to look like the one to the right...
Modify the Choice Logic: Add two new options under your existing if/elif for updating:
Option 2 (Delete):
Show card_keys, ask for a stat to delete.
Use del card[key] to zap it, then remove the key from card_keys.
Print the updated card.
Option 3 (Pop):
Show card_keys, ask for a stat to pop.
Use card.pop(key) to remove it and grab the value, then remove the key from card_keys.
Print the popped value and updated card.
Error Handling:
Wrap the new options in your existing try/except to catch KeyError if they pick a wrong stat.
Final Touch:
Print card_keys after every change so they see the stat list shrink!
SAMPLE OUTPUT SHOWN BELOW:
Delete Option
Pop Option
Stat Trade:
Pop a stat, then use its value to update another stat (e.g., pop 'attack', add it to 'hp').
Mass Zap:
Add a secret Option 4 to clear() the card and empty card_keys—total card meltdown!
Please create the file CH08_02c_Checkpoint.py
TASKS:
Start with 3 keys:
The dictionary initially has 'name', 'color', and 'height'.
Add 2 new keys: 'favorite_food' and 'number_of_eyes'.
Modify 3 existing values: The values of 'color', 'height', and 'name' are modified. 'height' must be modified with update
Delete 1 key: The 'favorite_food' key is removed using del.
Pop 1 key: 'number_of_eyes' is popped from the dictionary.
Pop a non-existent key: We try to pop 'weapon', which doesn't exist, and we handle it gracefully by returning 'Not found'.
AFTER EVERY CHANGE, YOU MUST PRINT THE DICTIONARY!!!
This is the start code you're given...