BYU logo Computer Science

To start this assignment, download this zip file.

Lab 5b — Building Dictionaries

Reminder, you should work in teams of 2 or 3 when solving the lab problems. Learning to code together is an important part of this class.

Preparation

1 minute

Download the zip file for this lab, located above. This zip file has code that you will use for this assignment. Extract the files and put them in your cs110 directory in a folder called lab5b.

Review on dictionaries

Here is a short reminder of how to create dictionaries, from the guide on building dictionaries:

# create an empty dictionary
meals = {}

# add a key/value pair to the dictionary
meals['breakfast'] = 'yogurt, granola, and fruit'

You will also want to remember the basics of using dictionaries from the guide on dictionaries:

# get the value for a key
food = meals['dinner']

# loop through all the key, value pairs
for meal, description in meals.items():
    print(f'For {meal} we are serving {description}. 😋')

# check if a key exists
if 'dinner' in meals:
    print("Yay, we are eating dinner.")

Practice

10 minutes

We have given you code in practice.py that provides some simple practice problems. You will see items 1 through 4:

  1. Create an empty dictionary named movie_ratings

  2. Add these entries to the dictionary:

    • “Toy Story 2” → “100%”
    • “Toy Story” → “100%”
    • “Finding Nemo” → “99%”
    • “Inside Out” → “98%”
    • “Toy Story 3” → “98%”
  3. Write a loop that prints The rating for {movie} is {rating} using the list movies = ["Toy Story", "Toy Story 2", "Toy Story 3"]

  4. Write a loop that prints The rating for {movie} is {rating} for ALL of the movies, by iterating over all of the (movie, rating) tuples in the entire dictionary.

Here is what the output of the program should look like if you have done this correctly:

The rating for Toy Story is 100%
The rating for Toy Story 2 is 100%
The rating for Toy Story 3 is 98%

The rating for Toy Story 2 is 100%
The rating for Toy Story is 100%
The rating for Finding Nemo is 99%
The rating for Inside Out is 98%
The rating for Toy Story 3 is 98%

Discuss with the TA:

  • Show a solution.
  • Is there anything you don’t understand about creating and using dictionaries?

Calories

15 minutes

We have given you some code in calorie_lookup.py that is supposed to do the following:

  • read a CSV file that contains lines of foods that list “name,calories”
  • put these into a dictionary that maps foods to calories
  • use an input loop to ask you to type in a food and then it tells you how many calories are in that food

Tasks

  1. Review the code that is written for you to see what it does.

  2. To complete this code, you need to write one function called read_csv(filename):

def read_csv(filename):
    """
    Reads the CSV file with the given filename. It should
    have lines that are of the format "food,calories". This
    function returns a dictionary that maps food -> calories.
    """
    # write code here
    pass

We have given you a file called cereal-calories.csv that contains a bunch of cereals and their calories per serving. The first few lines look like this:

Apple Cinnamon Cheerios,110
Basic 4,130
Cheerios,110
Cinnamon Toast Crunch,120
Clusters,110
Cocoa Puffs,110

After you write this code, run the program with:

python calorie_lookup.py cereal-calories.csv

You should see something like this:

Food: Count Chocula
Count Chocula has 110 calories per serving
Food: Apple Jacks
Apple Jacks has 110 calories per serving
Food: Life
Life has 100 calories per serving
Food:

Discuss with the TA:

  • How did you implement this function? Show a solution and discuss.
  • Is there anything you don’t understand about dictionaries?
  • Make sure everyone understands every line of code we supplied, including the input loop.

Team assignments

15 minutes

We have given you some code in team_assignments.py that is supposed to assign teams for a hacking competition. The code should do the following things:

  • read a file that contains a list of names of players
  • remove all the newlines from those lines
  • create a dictionary that maps the player names to the team
    • when it does this it assigns each player to a random team
    • the team names are “Y Hackers”, “Clever Cougars”, “We Did Nothing”
  • uses an input loop to ask you to if you want to enter a player p or a team t
  • if you want to enter a player:
    • read the name of the player
    • print the name of the team the player is on
  • if you want to enter a team:
    • read the name of the team
    • print all of the players for the team, in a bulleted list

Tasks

  1. Review the code that is written for you to see what it does. Pay special attention to strip_newlines(). This function will take a list of lines and remove all the newlines. You may find this handy for the homework!

  2. Write the function called assign_teams(players):

def assign_teams(players):
    """
    players -- a list of player names (strings)

    Returns a dictionary that maps a player name to a team.
    Team assignments are randomly chosen from "Y Hackers", "Clever Cougars",
    and "We Did Nothing"
    """
    # Write code here
    pass
  1. Write the function called do_player(assignments):
def do_player(assignments):
    """
    assignments -- a dictionary that maps player to team

    Asks for a player name. Then prints out the team that player is assigned to.
    The format is:
    {player} is on {team}
    If the player is not in the dictionary, then the program prints:
    Sorry, no player by that name.
    """
    # Write code here
    pass
  1. Write the function called do_team(assignments):
def do_team(assignments):
    """
    assignments -- a dictionary that maps player to team

    Asks for a team. Then prints out a bulleted list of all of the players
    on that team. The format is:
    * {player}
    """
    # Write code here
    pass

We have given you a file called players.txt that contains the names of some players. The start of the file looks like this:

Elizabeth
José
Mykala
Akari
Stephen

After you write the code, run the program with:

python team_assignments.py players.txt

You should see something like this:

Player or Team? (p/t) p
Player: Emma
Emma is on Clever Cougars
Player or Team? (p/t) p
Player: Joshua
Joshua is on We Did Nothing
Player or Team? (p/t) t
Team: Y Hackers
* Elizabeth
* José
* Artemis
* Junior
* Gordon
* Xinru
* Daniel
Player or Team? (p/t) t
Team: Do Nothing
Player or Team? (p/t) p
Player: John
Sorry, no player by that name.
Player or Team? (p/t) t
Team: We Did Nothing
* Felicity
* Joshua
* Warda
Player or Team? (p/t)

Grading

To finish this lab and receive a grade, take the canvas quiz.

Solution

We are providing a solution so you can check your work. Please look at this after you complete the assignment. :-)