3. Text-based quiz games

These programs can be entered using any text editor, but I suggest using the Mu editor because it comes with Python and libraries all pre-installed in one easy download.

3.1. Hello, world

The traditional first program used to make sure Python is working and that we can run programs.

If using the Mu editor:

  1. Click the mode button and make sure the mode is set to Python3.

  2. Type in the program.

  3. Click Save and enter a name for the program.

  4. Click Run.

Program 3.1 Hello, world
1print("Hello world")
2
3# This line is a comment, you dont have to type these!

3.2. Getting input from the keyboard

This program will pause and wait for you to enter some text with the keyboard, followed by the return key. The text you enter is stored in a variable, x.

Program 3.2 Getting input from the keyboard
1print("Enter your name:")
2x = input()
3print("Hello", x)
4if x == "richard":
5    print("That is a very cool name")

Exercise

Add some names of your friends and display a different message for each friend.

3.3. Making decisions: if, elif, else

This is how to add another name to Program 3.2

Program 3.3 Decisions: if, elif, else
1print("Enter your name:")
2x = input()
3print("Hello", x)
4if x == "richard":
5    print("That is a very cool name")
6elif x == "nick":
7    print("That is a rubbish name")
8else:
9    print("I do not know your name", x)

Program 3.3 is very similar to Program 3.2. The new lines have been highlighted. You can either modify Program 3.2, or else create a new file and use copy and paste to copy the code from the old program into the new.

3.4. A random maths question

Program 3.4 A random maths question
 1import random
 2
 3n = random.randint(0, 10)
 4
 5print("What is", n, "plus 7?")
 6g = int(input())  # Why do we use int() here?
 7if g == n + 7:
 8    print("Correct")
 9else:
10    print("Wrong")

Exercise

Add some more questions, e.g.

  • Instead of 7, use another random number.

  • Use a bigger random number.

  • Multiply (use *), divide (use /) or subtract (use -) numbers.

Exercise

Print how many questions the player got correct at the end.

3.5. Keeping score

We create a score variable to record how many questions the player answered correctly.

Program 3.5 Keeping score
 1score = 0
 2
 3print("What is 1+1 ?")
 4g = int(input())
 5if g == 2:
 6    print("Correct")
 7    score = score + 1
 8
 9print("What is 35-25 ?")
10g = int(input())
11if g == 10:
12    print("Correct")
13    score = score + 1
14
15print("Your score:", score)

3.6. Guessing game with a loop

This while loop goes round and round forever … or until the player gets a correct answer, and then it breaks out of the loop. Note that everything in the loop is indented.

Program 3.6 Guessing game with a loop
 1import random
 2
 3n = random.randint(0, 10)
 4
 5while True:
 6    print("I am thinking of a number, can you guess what it is?")
 7    g = int(input())
 8    if g == n:
 9        break
10    else:
11        print("Wrong")
12print("Correct!")

Exercise

Give a hint to the player when they are wrong. Was their guess too high or too low?

Exercise

Print how many guesses they took to get it right at the end.

3.7. Improved guessing game

Program 3.6 with a hint whether the guess is greater or lesser than the answer.

Program 3.7 Improved guessing game
 1import random
 2
 3n = random.randint(0, 100)
 4guesses = 0
 5
 6while True:
 7    guesses = guesses + 1
 8    print("I am thinking of a number, can you guess what it is?")
 9    g = int(input())
10    if g == n:
11        break
12    elif g < n:
13        print("Too low")
14    elif g > n:
15        print("Too high")
16print("Correct! You took", guesses, "guesses.")