Three Python Games for Beginners: Guess, Dice and Times Tables
These three programs are the shortest way to a working game. Each one is under 20 lines, uses only the standard library, and prints something on screen immediately.
Guess the number
import random
answer = random.randint(1, 10)
tries = 0
while True:
guess = int(input("Guess 1-10: "))
tries += 1
if guess == answer:
print("Correct in", tries, "tries!")
break
print("Higher" if guess < answer else "Lower")
Sample output
(run it and you will see the prompt printed in the terminal)
Dice roller
import random
rolls = [random.randint(1, 6) for _ in range(5)]
print("Rolls:", rolls)
print("Total:", sum(rolls))
Sample output
(run it and you will see the prompt printed in the terminal)
Multiplication drill
import random
score = 0
for _ in range(5):
a, b = random.randint(2, 9), random.randint(2, 9)
if int(input(f"{a} x {b} = ")) == a * b:
score += 1
print("You got", score, "out of 5")
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.