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
  1. Make sure you download the appropriate version of Python 3.5.1 for your system.
  2. Open up the program, which should look like this:
  3. 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.
  4. Save your file.
  5. 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!
  6. Enter the following text:
    P1115.jpg
  7. 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.
  8. 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.
  9. Test your program. What happens when you get the answer right? What happens when you get it wrong?
    P1116.jpg
    The indented code (that prints the smiley faces) only runs if the answer is correct. But “Thank you for playing!” always appears, whether your answer is right or wrong. Can you guess why that is?
    P1117.jpg
    Python uses two equals signs == to check if two things are the same. This is because one equals sign = is used to store something in a VARIABLE [for example, answer = input()].
    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.
  1. Enter the following text:
    P1118.jpg
  2. Try out this updated code. What happens when you enter the right answer now in the Python Shell?
  3. What happens when you enter anything else?
  4. 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.

  1. 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:
    P1119.jpg
    So why is this? Well, this happens because Python is very strict when it compares the player’s answer to the correct answer. To Python, “P” isn’t the same as “p”, and so if the player uses any capital letters in their answer, Python thinks the answer’s wrong. Test this out in your own game to see what happens.
  2. 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()
    P1120.jpg
    Now test your quiz again. Have you fixed the problem? Try testing these examples to see what happens:
    P1121.jpg

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.

  1. Enter the following text:
    P1122.jpg
    ELIF is short for “else if”. So in the program above, the player sees one of four messages, depending on what they entered for their answer.
  2. Test this new question four times, so that you see each of the different messages. It should look like this:
    P1123.jpg
  3. 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?
  4. 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.
    Your code should look like this:
    PracticeIT_2_1124.jpg
    And your program should look like this:
    P1125.jpg
  5. 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!)
Skills practised
  • Understanding algorithms
  • Applying algorithmic thinking

As a class, discuss the following questions:

  1. How is Python an example of how computer programs run using coding?
  2. In what way does algorithmic thinking influence how you use Python?
×
P1115.jpg
×
P1116.jpg
×
P1117.jpg
×
P1118.jpg
×
P1119.jpg
×
P1120.jpg
×
P1121.jpg
×
P1122.jpg
×
P1123.jpg
×
PracticeIT_2_1124.jpg
×
P1125.jpg
×
P1113.jpg
×
P1114.jpg