GithubHelp home page GithubHelp logo

kpmcdonough49 / dictionary_exercise_april2021 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from learn-co-students/dictionary_exercise_april2021

0.0 0.0 0.0 286 KB

Jupyter Notebook 89.17% Python 10.83%

dictionary_exercise_april2021's Introduction

Pokemon/Dictionary Warmup

Pokeball

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

Please answer the following questions below.

What datatype is the data above?

What else can you say about the data variable?

Variable Assignment & Data Types – Practice

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:

  1. Bayleef
  2. Haunter
  3. Poliwag
  4. Pidgeotto
  5. Kadabra

Please create the following variables given the information above:

  • 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

Looping is the bread and butter of code. Code is powerful because of it's speed and is especially skilled at completing repetitive tasks.

A simple for loop

Let's loop over the joels_pokemon_names list and print out each of the pokemon.

# Your code here

Looping over a dictionary

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 the data variable.
# Your code here

How many keys are in this dictionary?

In the cell below, set the variable pokemon_total to the number of keys in the dictionary.

# Your code here

Simple loop over dictionary

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

Let's create a new dictionary called joels_pokemon.

To do this, we will:

  1. Create an empty dictionary called joels_pokemon
  2. Loop over the names in the joels_pokemon_names list
  3. Use the name to access the pokemon's information in the data dictionary
  4. Add the pokemon name to the joels_pokemon dictionary as a key and the pokemon's information as the key's value
# 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()

Visualization

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:

  1. Isolate Bayleef's weakness data from our dictionary
  2. Identify any type that has a weakness score of 2 and append those types to a list called weakness_types
  3. Loop over our entire dataset
  4. 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

  1. 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:

  1. Creating an empty list
  2. Looping over the keys of our newly made dictionarybayleef_weakness_score.
    • Each key is a pokemon type
  3. Checking if the score equals 2
  4. 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.

Now your turn.

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()

dictionary_exercise_april2021's People

Contributors

j-max avatar joelsewhere avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.