Lab 11: Jeopardy!

In this part of lab, a JSON file with a list of 200,000+ Jeopardy (an American trivia TV show) questions and answers is in your lab11 folder. Each question/answer pair is stored in a dictionary.

The keys of these dictionaries are as follows:

Here is a sample dictionary from the list of dictionaries (formatted for easy reading here):

{  'category': 'SCIENCE CLASS', 
   'air_date': '2010-07-06', 
   'question': "'Lava & igneous rock are formed from this hot liquid rock material found under the earth's crust'", 
      'value': '$2000', 
     'answer': 'magma', 
      'round': 'Double Jeopardy!', 
'show_number': '5957'}

Overview

You'll start with a function getJeopardyList to read the data and return a list of dictionaries. If you call `getJeopardyList('jeopardy.json') it will return the data in the right format for further processing.

Next, you'll write a function to build a new, category-based dictionary. Once you build that dictionary, you can send it to a provided function to play a mini-version of Jeopardy.

Task 1. getJeopardyList

Write a function called getJeopardyList that takes the JSON file name as a parameter and returns the list of dictionaries. You'll need this import statement at the top of your file so you can use the json library.

import json

Hint: you'll need to use json.load to load in the json file.

Here are some sample tests of the getJeopardyList function:

>>> jeopardyList = getJeopardyList('whoops.json')
Oops, ran into a problem reading the file whoops.json

>>> jeopardyList = getJeopardyList('jeopardy.json')
>>> len(jeopardyList)
216930
>>> jeopardyList[800:803]
[{'category': "IT'S A DATE!", 'air_date': '2003-06-06', 'question':
  "'It's Bird Day in Oklahoma, & a popular date for pole dancing'",
  'value': '$800', 'answer': 'May 1', 'round': 'Jeopardy!', 'show_number': '4335'},
 {'category': '"J" WHIZ', 'air_date': '2003-06-06', 'question':
  "'This 1847 novel takes place mainly at Lowood Orphan Asylum & Thornfield Hall'",
  'value': '$800', 'answer': '"Jane Eyre"', 'round': 'Jeopardy!', 'show_number': '4335'},
 {'category': 'WEATHER WORLD', 'air_date': '2003-06-06', 'question':
  "'The Australian mountains that include the Charlotte's Pass ski area, or the weather there in July'",
  'value': '$1,000', 'answer': 'Snowy', 'round': 'Jeopardy!', 'show_number': '4335'}
]

Task 2. Plan to Organize by Categories

The JSON data stores questions as a list where each question has a category key. But when playing the game, the participant selects a category and then a question from that category is given to them (we're ignoring the question values for now). It would be useful in our code to first re-organize the questions into categories, so that we could quickly access all questions from a specific category, given the category name.

For this part of the lab, design a function to do that, and write just the function header and docstring for your design (don't write the code). Your function should take the jeopardyList produced by getJeopardyList as its only argument, and should return some new kind of data structure where it's easy to retrieve all questions associated with a specific category. Discuss with your partner what combination of dictionaries, lists, and/or other data structures we've learned about might make this work, and what the pros and cons are of this reorganization.

Task 3. makeCategoryDict

Although it may not match up exactly with your design above, we've designed a specification for a makeCategoryDict function which reorganizes things as explained in the previous task. Implement this version so that you can continue with the lab:

Write a function called makeCategoryDict that takes the jeopardy list built by getJeopardyList above and builds and returns a new dictionary. This new dictionary has categories (there are 27,995 categories) as keys, and the values are lists of tuples, where each tuple contains a question and an answer from that category.

>>> jeopardyCategoryDict = makeCategoryDict(jeopardyList)

>>> len(jeopardyCategoryDict)
27995

>>> len(jeopardyCategoryDict['HISTORY'])
349

Here is a partial list of the contents of the jeopardyCategoryDict['HISTORY'] value (which is a list of 349 tuples):

[("'For the last 8 years of his life, Galileo was under house arrest for espousing this man's theory'", 'Copernicus'), 
 ("'Built in 312 B.C. to link Rome & the South of Italy, it's still in use today'", 'the Appian Way'), 
 ("'In 1000 Rajaraja I of the Cholas battled to take this Indian Ocean island now known for its tea'", 'Ceylon (or Sri Lanka)'), 
 ("'Karl led the first of these Marxist organizational efforts; the second one began in 1889'", 'the International'), 
 ('\'This Asian political party was founded in 1885 with "Indian National" as part of its name\'', 'the Congress Party'), 
 ...
 ("'In 1935 the Saar territory was reunited with this country'", 'Germany'), 
 ("'Juan Vincente Gomez was dictator of this South American country in 1918 when it first exploited its oil'", 'Venezuela'),
 ...
]

Here is the complete list of the contents of the jeopardyCategoryDict['A DAY WITH GRANDMA'] value (which is a list of 5 tuples):

>>> jeopardyCategoryDict['A DAY WITH GRANDMA']
[("'You stopped for some alpaca chunky, grandma's favorite type of this wool knitting essential'", 'yarn'), 
 ("'Grandma slipped you a $10 bill, the redesigned one with this new fire-bearing symbol of freedom on it'", 'the torch of the Statue of Liberty'), 
 ("'It was awkward when Grammy asked about your sister's this--you thought she meant archery, but she meant a boyfriend'", 'a beau'), 
 ("'Had to hit the drugstore for grandma's pills for this rash disease common in the elderly, aka herpes zoster'", 'shingles'), 
 ("'Back to the drugstore--grandma forgot her vacation photos from Pike County in these NE Pennsylvania mts.'", 'the Poconos')
]

Task 4. Explain playJeopardy

You are provided with a slightly buggy function called playJeopardy that plays an imperfect mini-version of the game. playJeopardy takes 3 parameters as follows:

To understand what the code is trying to achieve, go through the code with your partner and add comments to each line, explaining what that line of code is doing in the overall function. If you spot any bugs, make note of them.

Task 5. Debug playJeopardy

To play jeopardy, you call the playJeopardy function.

Notes:

Here is a sample game (with 4 specified categories and 3 rounds):

playJeopardy(jeopardyCategoryDict, ['FASHION', 'ALMA MATERS', 'FACT OR FICTION?', 'KIDSPEAK'], 3)
Welcome to Jeopardy! I will be your host today.
You can choose from these categories:  ['FASHION', 'ALMA MATERS', 'FACT OR FICTION?', 'KIDSPEAK']
Which category? fashion
'In the late 1800s, gowns named for this hot beverage were worn at afternoon gatherings'

Please type your answer here: coffee
Sorry, you were wrong, the correct answer is tea
Which category? alma maters
'Lady Bird Johnson & John B. Connally went to the University of this state'

Please type your answer here: Texas
Correct!
Which category? Kidspeak
"They're also called milk teeth & you probably began to lose yours when you were about 6"

Please type your answer here: baby teeth
Correct!
------------------------------
Your score: 0 answer(s) correct out of  3 (0.0%)
>>> 

Here is a sample game (with 4 specified categories and 5 rounds):

>>> playJeopardy(jeopardyCategoryDict, ['FASHION', 'ALMA MATERS', 'FACT OR FICTION?', 'THE OSCARS'], 5)
Welcome to Jeopardy! I will be your host today.
You can choose from these categories:  ['FASHION', 'ALMA MATERS', 'FACT OR FICTION?', 'THE OSCARS']
Which category? alma maters
'Presidential kids are welcome at this New Orleans university: both Neil Bush & Amy Carter hold degrees from it'

Please type your answer here: Tulane
Correct!
Which category? fashion
'A traditional trench coat features these accessories, from the Latin for "shoulder blade"'

Please type your answer here: epaulet
Correct!
Which category? alma maters
'Brad Pitt & Sheryl Crow were classmates at this school, Mizzou for short'

Please type your answer here: Missouri State
Sorry, you were wrong, the correct answer is the University of Missouri
Which category? the oscars
He said, "I swear I have never held one before" when he accepted his Best Director Oscar for "Schindler's List"

Please type your answer here: Steven Spielberg
Correct!
Which category? alma maters
'Larry Page & Sergey Brin started Google in their dorm room while working on Ph.D.s at this California college'

Please type your answer here: Stanford 
Correct!
------------------------------
Your score: 0 answer(s) correct out of  5 (0.0%)

Your task: Debug playJeopardy

Fix the function so it correctly displays an accurate count of correct answers.

Table of Contents