Trivia
Recall that during lab we implemented a program called trivia0.py
that
- Contained a set of question-answer pairs inside of a dictionary
- Prompted user to respond to trivia questions
- Updated the score based on the correctness of the answer
- Printed out the final score!
Now, your goal will be to implement a trivia game using OpenAI API!
Before You Begin
If haven’t yet, follow the instructions on Getting Started page to download the neccesary files.
After completing all the steps, you should find that your terminal window’s prompt resembles the below:
lab1/ $
Then, execute
cd trivia
followed by Enter to move yourself into (i.e., open) that directory. Your prompt should now resemble the below.
lab1/trivia/ $
You can now execute
code trivia.py
to open a file called trivia.py
where you’ll complete the rest of the program.
Implementation
In a file called trivia.py
, implement a program that will play trivia game with a user using OpenAI API to generate and evaluate the questions.
Most of the program is already implemented for you! Take some time to read and understand the code. Note that by default the program will ask 5 questions but you are welcome to change the number.
Now, your goal is to:
- Complete a helper function
prompt
that prompts the user to type an answer to question. Take care to return that answer! - Complete a helper function
score_update
which accepts two arguments:evaluation
that is an evaluation generated using OpenAI API based on the user’s answer andscore
that is a variable that stores the number of correctly answered questions.score_update
checks whether the evaluation indicates that the answer is correct and if it is, update the score accordingly.score_update
returns the updated score
- In
question_prompt = "TODO"
replace"TODO"
with a prompt to generate a question. - In
evaluation_prompt = ("TODO")
replace"TODO"
with a prompt to evaluate user’s answer based on the question just aksed,- Test out different prompts!
Hints
- Recall that you can use function
input
to prompt user for an input, per docs.python.org/3/library/functions.html#input. - Recall that you can check if a specific word is present in a phrase by using
in
keyword, i.e.if "cat" in phrase
How to Test
Here’s how to test your code manually:
- Run your program with
python trivia.py
. You should see a question printed in the terminal, followed by a prompt for user to respond. Type an answer to the question and click Enter. You should, then, see an evaluation printed in the terminal.
There are no universal outpust as each question and evaluation will depend on the prompts that you pick!