G1: Animal Quiz
Are you a fan of quizzes? Would you like to make one yourself? In this project, you’ll build an animal quiz. Even though the questions are about animals, this project can be easily modified to be about any other topic.
In this example, you’ll see bits of code with annotations. The code in black is new code to be added. The code in grey is existing code; use this to work out where to add the new lines of code. The instructions will ask you to run the code at various points. Make sure you do this and that the code successfully runs. Do not proceed to the next step until the previous one works. You’ll just make things harder for yourself!
The program asks the player some questions about animals. They get three chances to answer each question—you don’t want to make the quiz too difficult! Each correct answer will score one point. At the end of the quiz, the program reveals the player’s final score.
This project makes use of a function—a block of code with a name that performs a specific task. A function lets you use the same code repeatedly, without having to type it all in every time. Python has lots of built-in functions, but it also lets you create functions of your own.
It’s now time to build your quiz! First you’ll create the questions and the mechanism for checking the answers. Then you’ll add the code that gives the player three attempts to answer each question.
Open Mu Editor. Make sure the editor is in the right mode. Click the Mode button in the top left, and choose Python 3. Create a new file, and save it with the name animalQuiz.py
in your Digital Tech folder, in a folder named quiz
.
Type in the code shown here to create a variable called score
and set its starting value to 0
.
Next, create a message to introduce the game to the player. This will be the first thing that the player sees on the screen.
Now try running the code. Press the Run button from the bar at the top. What happens next? You should see the welcome message in the interactive panel at the bottom of the window. Press the Stop button (where the Run button was) to stop the program and return to editing.
The next task is to check if the player’s guess is correct. Type this code at the top of your script, before score = 0
. The code creates a function, called check_guess()
, that will check if the player’s guess matches the correct answer. The two words in brackets are parameters—bits of information the function needs. When you call (run) a function, you assign (give) values, known as arguments, to its parameters.
What happens if the player types “Lion” instead of “lion”? Will they still get a point? No, the code will tell them it’s the wrong answer! To fix this, you need to make your code smarter. Python has a lower()
function, which changes words into all lower-case characters. In your code, replace if guess == answer:
with the line shown on the right in bold.