Dictionaries #
In this lab, we will learn about dictionaries, another useful data structure.
[0] Setup #
π»
Start by cloning your lab-dictionaries repository in your cs9 folder.
cd desktop/cs9/unit_01
git clone https://github.com/the-isf-academy/lab-dictionaries-YOUR-GITHUB-USERNAME.git
[1] High π Useful Data Structure #
You already know about lists, which are a great way to store things that naturally come one after another, like subway stops on a subway line, or homework assignments in a class.
The structure of a list works well for things that have a natural order (like zodiac cycles), but what about things that don’t have a natural order to them?
A dictionary is another kind of data structure that is useful for information that does not have a natural order.
Dictionaries connect keys to values. For each unique key (for example, an animal name like 'pig'), a dictionary stores a unique value (like a translation ηͺ).
animal_dict = {
"rat": "ιΌ ",
"ox": "η",
"tiger": "θ",
"rabbit": "ε
",
"dragon": "ιΎ",
"snake": "θ",
"horse": "马",
"sheep": "ηΎ",
"monkey": "η΄",
"rooster": "ιΈ‘",
"dog": "η",
}
Values in a dictionary are accessed via the key. For example:
animal_dict['dog']
Will return:
'η'
Keys can easily be added to a dictionary like so:
animaion_dict["pig"] = "ηͺ"
You can loop through a dict in almost the same way you can loop through a list:
for english, chinese in animal_dict.items():
print("English zodiac: {} - Chinese zodiac: {}.".format(english, chinese))
Will output:
English zodiac: rat | Chinese zodiac: ιΌ . English zodiac: ox | Chinese zodiac: η. English zodiac: tiger | Chinese zodiac: θ. English zodiac: rabbit | Chinese zodiac: ε . English zodiac: dragon | Chinese zodiac: ιΎ. English zodiac: snake | Chinese zodiac: θ. English zodiac: horse | Chinese zodiac: 马. English zodiac: sheep | Chinese zodiac: ηΎ. English zodiac: monkey | Chinese zodiac: η΄. English zodiac: rooster | Chinese zodiac: ιΈ‘. English zodiac: dog | Chinese zodiac: η. English zodiac: pig | Chinese zodiac: ηͺ.
[Zodiac Year] #
π» Let's run the file
zodiac_year_finder.py. It’s inside the /introduction directory. Currently, it only tell you your zodiac in English.
> python3 zodiac_year_finder.py
What is your birth year? 2010
I was born in the year of the tiger.
π» Use the
animals_dict to add the Chinese translation to the English Sentence “I was born in the year of the ________”.
Your
zodiac_year_finder.pyshould now output something like:I was born in the year of the tiger. ζε±θ
[2] Game Library #
Let’s imagine our school had a library of video games. We could use a dictionary and lists to organize each game by genre.
game_library_dict = {
"Sports": ["Fifa", "NBA 2K", "Wii Sports"],
"Puzzle": ["Sodoku", "Tetris", "Bejeweled","Mahjong"],
"Multiplayer": ["Amoung Us","Fall Guys","Minecraft","Fortnite","Rocket League"],
"RPG" : ["The Witcher 3","Skyrim", "World of Warcraft", "Persona 5", "The Legends of Zelda"]
}
Notice:
- each
keyis astringtype- each
valueis alistofstringtypes
It’s up to you, to write functions that will return information about the game_library dict.
You will code the following functions in the library_functions.py:
total_num_games()get_genres()num_games_per_genre()get_games_for_genre()
π»
total_num_games()
- Paramter: a dictionary
- Return value: an integer value representing the number of games in the given dictionary
π»
get_genres()
- Paramter: a dictionary
- Return value: a list of the genres available in a given dictionary
π»
num_games_per_genre()
- Paramters: a genre, a dictionary
- Return value: an integer value representing the number of games available for the given genre in the given dictionary
π»
get_games_for_genre()
- Paramter: a genre, a dictionary
- Return value: a list of games available for the given genre in the given dictionary
A helpful function:
| Function | Explanation | Example |
|---|---|---|
| keys() | returns the keys in a dictionary | my_dict.keys() |
[Testing] #
π» Write tests to ensure each of your functions works as intended.
Use library_test.py to test your functions sufficiently.
β CHECKPOINT:
Answer the following prompts in your notebook before submitting this lab:
- What is a dictionary and why is it useful?
- How would you use a dictionary to organize students by grade level?
- Look at the “0. Planet Gravity” Do Now. How could you use a dictionary to better organize the information?
[Deliverables] #
β‘β¨
π» For this lab, you should
pushupdates to the following files to Github.
lab-dictionariesrespository
zodiac_year_finder.pylibrary_functions.pylibrary_test.pyAlso be sure to hand in your notebook with checkpoint questions.
[3] Extensions: Interactive Games Library #
Now that you’ve written the library_functions, it’s up you to put them to good use.
π» Create a new file called,
interactive_library.py
- viewing the total number of games
- viewing the genres
- viewing the games by genre
π» Write a new file called,
interactive_library.py
Here is an example:
------------------------------------
--Welcome to the CS Game Library--
------------------------------------
We have 17 total games.
We have the following genres:
- Sports
- Puzzle
- Multiplayer
- RPG
Select a genre to view the games available: Sports
We have 3 Sports games:
- Fifa
- NBA 2K
- Wii Sports
------------------------------------
--------Thanks for visiting!--------
------------------------------------
π»
Be sure to push your new file to Github.
[Contributing to the library] #
How would you adjust your program to allow users to add games to the library?
π» Add a functionality that allows users to add games to the library.Consider:
- How would you implement a menu system?
- How do you add things to a dictionary?
π»
Remember to push your work to Github.