Joél is currently playing the Pokemon Crystal video game.
Joél takes pokemon very seriously so they have downloaded The Complete Pokemon Dataset from Kaggle and they need some help organizing the data.
Let's use our dictionary parsing skills to help Joél out!
In the cell below, we import the Pokemon Dataset.
Don't worry about this code for now. Just run the cell
import json
import os
path = os.path.join('data', 'pokemon.json')
with open(path, 'r') as fp:
data = json.load(fp)
Let's take a look at the data we just imported.
data
What datatype is the data above?
What else can you say about the data variable?
Variable assignment is important! Writing professional quality code will often times require you to be very thoughtful about how you assign your variables. You may be asked to define a variable with a specific name, or with a specific data type. In scenarios like this, if you were to give a variable the incorrect name or incorrect data type, the entire project could break!
The importance of how you define variables will be seen out at the end of this notebook where you will be asked to create variables that you will then submit to your instructor.
Let's take a look at some examples.
The following information is true about Joél's pokemon.
- Joél has caught
5
pokemon - Joél's pokemon trainer level is
'Apprentice'
. - Joél's coolest pokemon is
'Haunter'
. - Joél's favorite pokemon is
'Kadabra'
. - Joél plays pokemon for 1.5 hours a day.
Additionally, the names of Joél's five pokemon are:
- Bayleef
- Haunter
- Poliwag
- Pidgeotto
- Kadabra
pokemon_count
that has a datatype of integer.trainer_level
that has a datatype of string.coolest_pokemon
that has a datatype of string.favorite_pokemon
that has a datatype of string.hours_per_day
that has a datatype of float.joels_pokemon_names
that has a datatype of list.
# Your code here
The cell below tests whether or not you assigned your variables correctly!
from tests import VariableAssignment
test = VariableAssignment()
test.run(pokemon_count, trainer_level,
coolest_pokemon, favorite_pokemon,
hours_per_day, joels_pokemon_names)
Looping is the bread and butter of code. Code is powerful because of it's speed and is especially skilled at completing repetitive tasks.
Let's loop over the joels_pokemon_names
list and print out each of the pokemon.
# Your code here
To loop over a dictionary we will need to loop over the keys of the dictionary.
In the cell below,
- Assign the variable
data_keys
to a list containing the keys for thedata
variable.
# Your code here
In the cell below, set the variable pokemon_total
to the number of keys in the dictionary.
# Your code here
Now let's loop over the dictionary and save the top level value (i.e. everything but the key) to a list called pokedex
.
Note: This is just an exercise. pokedex
will not be used further.
# Your code here
# Your code here
To do this, we will:
- Create an empty dictionary called
joels_pokemon
- Loop over the names in the
joels_pokemon_names
list - Use the name to access the pokemon's information in the
data
dictionary - Add the pokemon name to the
joels_pokemon
dictionary as akey
and the pokemon's information as the key'svalue
# Your code here
Run the cell below to see if you successfully made the joels_pokemon dictionary!
from tests import CheckDictionary
test = CheckDictionary(joels_pokemon)
test.run()
Let's figure out which pokemon is Joél's strongest pokemon!
In the cell below
- Create a bar plot for that shows the attack stat for each of joel's 5 pokemon.
- Give the plot the following title:
Joel's pokemon stats
.
# Your code here
What if we wanted to sort the graph?
There is a couple ways we can do this, but a useful way of doing it would be to sort the dictionary itself!
sorted_dict = dict(sorted(joels_pokemon.items(), key=lambda item: item[1]['stats']['attack']))
attack_stats = [sorted_dict[pokemon]['stats']['attack'] for pokemon in sorted_dict]
names = list(sorted_dict)
plt.bar(names, attack_stats)
plt.title("Joél's pokemon stats");
Bayleef is Joél's strongest pokemon. Let's create a list of all Pokemon that Bayleef is weak against.
Our dictionary gives us the Types of pokemon Bayleef is weak to. Our dictionary also gives us the type for each pokemon. So first we have to figure out what types of pokemon Bayleef is weak to, then grab every pokemon that has that type.
Ok, here is how we will do it.
Weakness is measured with the weakness
key in our data. If the weakness value = 2, that means the pokemon is extremely weak to that type of pokemon.
To find all pokemon that Bayleef is weak to, we have to:
- Isolate Bayleef's weakness data from our dictionary
- Identify any type that has a weakness score of 2 and append those types to a list called
weakness_types
- Loop over our entire dataset
- Identify pokemon who have a type that match the types in our
weakness_types
list.
Hint The type of each pokemon can be found using the
stats
key
- Append the names of those pokemon to a list called
bayleef_weakness
Let's walk through steps 1 and 2.
You will code steps 3-5!
Step 1: Isolate Bayleef's weakness data from our dictionary
bayleef_weakness_scores = data['Bayleef']['weakness']
Step 2: Identify any type that has a weakness score of 2 and appending those types to a list called weakness_types
We do this by:
- Creating an empty list
- Looping over the keys of our newly made dictionary
bayleef_weakness_score
.- Each key is a pokemon type
- Checking if the score equals 2
- Appending the key to our empty list if the score equals 2
weakness_types = []
for weakness in bayleef_weakness_scores.keys():
if bayleef_weakness_scores[weakness] == 2:
weakness_types.append(weakness)
weakness_types
Bayleef is weak to ice, poison, bug, fire, and flying pokemon.
In the cell below, use the weakness_types list to identify pokemon that have one of those types, and append those pokemon to a list named bayleef_weakness
.
Hint The code will be very similar to the code for step 2.
Hint Make sure your list doesn't contain duplicates of the same pokemon!
# Your code here
Run the cell below to test your code! ⬇️
from tests import ListCheck
test = ListCheck(bayleef_weakness)
test.run()