A dictionary in Python is a collection of key-value pairs.
Each key in the dictionary is unique and is used to access its corresponding value.
Dictionaries are similar to lists, but instead of using an index (like 0, 1, 2, etc.) to access elements, you use a key (WORD)
Understanding dictionaries allows you to model a variety of real-world objects more accurately.
You’ll be able to create a dictionary representing a person and then store as much information as you want about that person. You can store their name, age, location, profession, and any other aspect of a person you can describe.
You’ll be able to store any two kinds of information that can be matched up, such as a list of words and their meanings, a list of people’s names and their favorite numbers, a list of mountains and their elevations, and so forth
The variable name you choose (e.g., suspect, card, chest).
Curly braces enclose the dictionary.
Each pair is a key (usually a string) and its value, separated by a colon :.
Separate each key-value pair (no comma after the last pair).
Can be any type—strings, numbers, Booleans, lists, even another dictionary!
To get a VALUE for a specific KEY in a dictionary, all we need to do is treat it like a list, but instead of the index, use the KEY NAME
If we wanted to get the user name from this dictionary, all we'd need to do is use:
We use the KEY-NAME, which is so much nicer than trying to remember an INDEX where we're storing the name!!!
Are there other ways? Of course, but for now, let's just look at this nice, simple way to access a key's value
There's an alternate way to do this, using the get( ) method!
If you're looking for a value, and the key does't exist, PASS A DEFAULT VALUE!!
What happens if we use a key which doesn't exist?
It will raise a KeyError
We use a try/except block to catch this!!!
You’re a player in Marvel Rivals, the epic 6v6 hero shooter! Pick a character from the game (e.g., Spider-Man, Black Panther, Scarlet Witch) and build a dictionary to track their in-game profile. Include 5-7 key-value pairs with these required fields (all single values):
'hero_name': String (e.g., "Spider-Man")
'real_name': String (e.g., "Peter Parker")
'role': String (e.g., "Duelist")
'health': Integer (e.g., 300)
'damage': Integer (e.g., 50)
'ability': String (e.g., "Web Swing")
'unlocked': Boolean (e.g., True)
You must now write a program that will offer the user the following options (shown to the right)
Option 1: Here's What The Print Looks like:
If the user selects option 1, just print the entire dictionary with a simple print statement.
Option 2: This requires some error catching...
If the user selections option 2, they're typing in the key to display
This means they could mistype something which ISN'T A KEY - You have to catch this!
If you don't properly catch this, your program would crash!
ADVANCED TASK 1: Loop until the user selects a working Key
ADVANCED TASK 2: Display the keys, and just let the user select from a list!
This avoids the KeyError exception
Valid Input:
Invalid Input:
You’re the strategist for a Marvel Rivals squad, managing a trio of heroes for the next big match! In this activity, you’ll build a team by creating a list of three character dictionaries, each packed with their in-game stats. Then, you’ll craft a menu to either peek at one hero’s details or compare a stat across the whole team. Use your dictionary skills to keep this roster in check—let’s assemble the ultimate lineup!
Goal:
Create a list of three hero dictionaries, select a team member to display a stat, and compare a stat across all members.
Create three hero dictionaries (hardcode them with different Marvel Rivals characters).
Store them in a list called team.
Display a Menu
Prompt for a choice (1, 2, or 3):
Option 1:
Loop through team and print each hero’s dictionary.
This can be done with a simple for loop and print statement.
You don't need to get any specific key, etc...
Option 2:
Ask for a hero name and a stat key, loop to find the hero, and print the stat value (use try/except for errors).
Remember, you're doing this for only 1 character.
User must supply the character name, or even better, let them choose the name from a list!!
ERROR CHECK!!!!
Option 3:
Ask for a stat key, loop through team, and print that key’s value for all heroes (use try/except if the key’s missing).
You can also display a list of the keys (hardcoded) that a user can pick from!