11.5 Simple coding in Python
Learning to code is literally like learning a new language (learning to construct sentences, etc.). In the following exercise, you will learn how to code a simple multiple-choice game in Python, with the aim of giving a general introduction to the syntax (specific language) and functionality of Python. For further activities and tips visit the Python site, where we recommend the Beginner’s Guide to Python.
Next, we will make a simple quiz in Python, which you can use to challenge your classmates.
Algorithms and programming exercise 7
Using Python to create a quiz
Skills practised
- Creating a Python project
- Navigating the Python Shell
- Python syntax
- Make sure you download the appropriate version of Python 3.5.1 for your system.
- Open up the program, which should look like this:
- Next, open a new file. You should have two windows now, one is called the Python Shell and the other is the untitled new document. The Shell is where you will eventually run the programs you write in Python. The currently untitled new document is where you write your code.
- Save your file.
- Let’s start by writing a very simple quiz program that asks the player a question, then if they input the correct answer they are rewarded with 100 smiley faces!
- Enter the following text:
- Be sure to add the colon : to the end of the line
if answer == “print”:
And also indent the line below it (move it to the right) with spaces or the tab button. - Save your file, then hit F5 to run your program in the Python Shell. Tip: Whenever you want to run your program, remember to first save your file.
- Test your program. What happens when you get the answer right? What happens when you get it wrong?
The program currently prints smiley faces if the player gets the question right, but doesn’t print anything to tell them they got the answer wrong. You can add an ELSE statement to print sad faces if the user inputs anything other than the correct answer.
- Enter the following text:
- Try out this updated code. What happens when you enter the right answer now in the Python Shell?
- What happens when you enter anything else?
- Challenge: Use what you’ve learnt so far to create your own quiz. You can choose any topic you like, but ensure your quiz uses IF and ELSE statements to let the player know how they’re doing.
Algorithms and programming exercise 8
Testing code in Python
Skills practised
- Creating a Python project
- Navigating the Python Shell
It’s always a good idea to test your programs to make sure that they work properly.
- When you tested your quiz, you may have noticed that it is possible to get sad faces even when you input a correct answer, like in this example below where the player has accidentally pressed CAPS LOCK:
- To fix this problem, you need to convert the player’s input to lowercase, so there are no capital letters in their answer. Make this following change to your code where the player inputs their answer:
answer = input() .lower()
Algorithms and programming exercise 9
Creating a multiple-choice quiz in Python
Skills practised
- Adding multiple questions
- Adding a score
So far you’ve used IF and ELSE to let the player know if they got the answer right or wrong. But how could we create a multiple-choice quiz, where the user could see one of four different messages depending on the answers they input? You can use an ELIF statement to do this.
- Enter the following text:
- Test this new question four times, so that you see each of the different messages. It should look like this:
- Challenge: Multiple-choice quiz – Test yourself by adding a few more multiple-choice questions to your quiz program. Once you have finished making your quiz, swap yours with a classmate! How did they do? Did they enjoy it? Was it too easy or too hard?
- Challenge: Keeping score – can you use a SCORE VARIABLE in your quiz program to keep track of the player’s score? This is how the VARIABLE could be used:
- At the start of the program, set the SCORE to 0.
- Whenever a question is answered correctly, add 1 to the player’s score. (score = score + 1)
- PRINT the player’s score at the end of the quiz.
- Challenge: Can you print a personalised message to the player at the end of the game? For example, make Python say “congratulations” if they got all of the questions right. Or else say “try again” if they got any wrong. (You’ll need to use your SCORE VARIABLE to decide which message to print!)
Algorithms and programming exercise 10
Class discussion
Skills practised
- Understanding algorithms
- Applying algorithmic thinking
As a class, discuss the following questions:
- How is Python an example of how computer programs run using coding?
- In what way does algorithmic thinking influence how you use Python?