Python Quiz Games: Score Keeping and a Word Jumble
Quiz games are exercises in storing answers in a list, comparing strings and keeping score. Both examples below run in the terminal.
Quiz with a score
questions = [
("What is 7 x 8?", "56"),
("Which planet is closest to the Sun?", "mercury"),
("How many legs does a spider have?", "8"),
]
score = 0
for q, a in questions:
if input(q + " ").strip().lower() == a:
score += 1
print("Score:", score, "of", len(questions))
Sample output
(run it and you will see the prompt printed in the terminal)
Word jumble
import random
word = "monster"
letters = list(word)
random.shuffle(letters)
print("Unjumble:", "".join(letters))
guess = input("Your answer: ")
print("Yes!" if guess.lower() == word else "It was " + word)
Sample output
(run it and you will see the prompt printed in the terminal)
Common mistakes
- Comparing
input()with a number:input()always returns text, so wrap it inint()before comparing. - Forgetting to import
randombefore callingrandom.randint(). - An infinite loop when the exit condition is never updated inside the loop.
Variations to try
- Add a score that survives between rounds.
- Let the player choose difficulty before the first round.
- Move the game into a function and call it again to replay without restarting the file.
More Python games
Overview of the whole set: Python games code to copy and paste.
-
- Three Python Games for Beginners: Guess, Dice and Times Tables
- Python Games With Player Input: Rock Paper Scissors and a Text Adventure
- Python Quiz Games: Score Keeping and a Word Jumble
If you prefer learning by playing, Code Monsters Academy teaches the same ideas one level at a time.