Our Python book, starting on page 188, introduces the Trivia Challenge program. For this project, we are going to make two of our own versions of that program.
To do this project, you will need to learn two important new concepts:
· Working with files that contain data (not included in the program itself).
· Handling exceptions that occur when your program encounters something unexpected. (For example, it tries to read a file that doesn’t exist.)
The first thing you should do is to read the following sections of Chapter 7:
· Pages 189 - 190 introduce the Trivia Challenge program.
· Pages 190 - 196 introduce the basics of opening files, reading from them, and closing them. You can skip pages 197 - 199, since we won’t need to write out anything. You can skip pages 200 - 204 since we will be using just simple text files.
· Pages 205 - 210 introduce exception handling. Don’t worry if you don’t get all the details. We’ll be starting with a program that already handles the exceptions that can occur when we try to read a file. You should just try to understand the basic idea.
· Pages 210 - 215 describe the Trivia Challenge program that we will be starting with. I will also go over this program in class. Make sure you are there.
Once you have done the reading, you’ll understand the role of the file trivia.txt. You’ll find the author’s version on the book’s website.
1. Start by creating your own trivia.txt file. Its format should be identical to that of the book’s version. You should have about ten questions. You can be as clever or as stodgy as you like in coming up with your questions. Store your file in the same directory in which you store your Python programs. (If you don’t, your program will not be able to find your file.)
2. Make your own copy of the author’s trivia_challenge module. In other words, copy it to your directory and make sure it has the name trivia_challenge.py.
3. Run your module. You will likely find that you’ve made some formatting errors in creating trivia.txt. Fix them.
Next we’ll augment the game as suggested in Challenge 1 on page 216 of the book. Our original game awards one point for every correct answer. Now we will add to the definition of each question a number that is the number of points that will be awarded when a user correctly answers the question.
1. To add this functionality, you will need to:
a. Create a new file trivia_points.txt. The format of this file will be the same as that for trivia.txt except that you need to add a number (the point value) to each question. You may do this any way you like, but probably the easiest is to put it on its own line, probably right after the explanation line.
b. Modify your code so that:
i. It reads the file trivia_points.txt rather than trivia.txt.
ii. Each time it reads a question it reads in the point value and stores it (in a variable). Important: exactly how you do this depends on the decision you made above in part a. Also, remember: when you read in a number, it is read as a string. You will need to be able to use it as a number (so you can add it to the score). The following code converts a string in the variable y to a number:
y = int(y)
iii. Each time a question is answered correctly, the score should be incremented by the given point value.
2. Call your function to make sure it works correctly.
These first two functions report the user’s final score, but they make no value judgement about it. Now we’ll change that.
1. Add to your module a new function judgement(score). The job of judgement is to check the user’s final score and print a message based on the value. Note that what you need to do here is very much like what you did in Part III of the Exclusive Networks program. As there, you will want to use the if/elif construction. Be as creative as you like with your messages. You should have at least five point ranges (and associated messages).
2. The last line of the main function prints the user’s final score. Add a new line that calls judgement(score).
1. Do Challenge 2 on page 216 of the Python book. Or:
2. Do something else that you think makes the game more interesting. For example, you might have many more questions. Then you could give the user ten at a time, then ask if he/she wants to keep going. If you decide to do anything extra, make sure that you document your code well with comments so that we can see what you’re doing.
Turning in Your Project
Use the turnin system to submit your code and your data file. You’ll do this with two separate submits.