''' Author: Purpose: CS111 Dictionaries + File Format practice using jeopardy json file (216930 questions) in the provided list of dicts Date: Fall 2021 Filename: jeopardy.py ''' import random # Write your Task 1 and Task 2 functions here # We are providing two functions: # The bug-free validateCategoryChoice function # and # the mildly buggy playJeopardy function below for Task 3 def validateCategoryChoice(cat, catList): '''Makes sure that the category is a valid one from the category list. ''' lowerCatList = [cat.lower() for cat in catList] while cat.lower() not in lowerCatList: print('Please enter a category from this list', catList) cat = input('Which category? ').lower() return cat.upper() def playJeopardy(categoryDict, categoryList, numRounds): '''Relies upon category dictionary to play the game for numRounds, using only specified categories in categoryList. ''' try: print("Welcome to Jeopardy! I will be your host today.") print('You can choose from these categories: ', categoryList) for round in range(numRounds): counter = 0 rawCategoryChoice = input('Which category? ').upper() categoryChoice = validateCategoryChoice(rawCategoryChoice, categoryList) questionAnswerCombo = categoryDict[categoryChoice] theQuestion, theAnswer = random.choice(questionAnswerCombo) print(theQuestion) print() userAnswer = input('Please type your answer here: ') if userAnswer.lower().strip() == theAnswer.lower().strip(): print('Correct!') else: print('Sorry, you were wrong, the correct answer is', theAnswer) print('-' * 30) print('Your score:', correct, 'answer(s) correct out of ',numRounds, '('+f"{correct/numRounds*100}%"+')') except KeyError: print('Sorry, that does not match one of my categories:', categoryList)