Create a game where the computer chooses a secret number and the player tries to guess it, with helpful clues along the way.
In this project you will build a classic game that programmers have been making for more than fifty years. The computer secretly picks a number between 1 and 20, and you have to guess it. After every guess the computer gives you a clue: "too high" or "too low". Keep guessing until you find it!
It is a perfect first project because it is small enough to finish in one sitting, but it still feels like a real game you can show your friends and family. Every time you play, the secret number is different, so the game never gets boring.
Along the way you will practise four big ideas: storing information in variables, asking the player questions with input(), making decisions with if, elif and else, and repeating code with a while loop. You will also meet the random module, which lets your programs surprise you.
Every good game starts with a welcome message. We use print() to show text on the screen. Run this first so you know Python is working before we add the fun parts.
print("đ˛ Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 20.")
đ˛ Welcome to the Number Guessing Game! I'm thinking of a number between 1 and 20.
The random module can choose numbers for us. random.randint(1, 20) picks a whole number from 1 to 20, and both ends are included. We store it in a variable called secret. For now we print it so we can check it works, but we will remove that line later so the player cannot cheat!
import random
secret = random.randint(1, 20)
print("Psst... the secret number is", secret)
Psst... the secret number is 8
đ˛ Your number will probably be different, because it is random!
input() shows a question and waits for the player to type an answer. Whatever they type comes back as text, so we wrap it in int() to turn it into a number. Now we can compare it with other numbers.
guess = int(input("Your guess: "))
print("You guessed", guess)
Your guess: 12 You guessed 12
Now the computer needs to make a decision. If the guess is smaller than the secret, we say "too low". If it is bigger, we say "too high". Otherwise, the only thing left is that they are equal, so the player wins! This is exactly what if, elif and else are for.
import random
secret = random.randint(1, 20)
guess = int(input("Your guess: "))
if guess < secret:
print("Too low! Try a bigger number.")
elif guess > secret:
print("Too high! Try a smaller number.")
else:
print("đ You got it! The number was", secret)
Your guess: 10 Too high! Try a smaller number.
One guess is not much of a game. A while loop repeats its code for as long as its condition is True. Here the condition is guess != secret, which means "the guess is not the secret yet". We start guess at 0 so the loop always runs at least once, because 0 can never be the secret.
import random
secret = random.randint(1, 20)
guess = 0
while guess != secret:
guess = int(input("Your guess: "))
if guess < secret:
print("Too low! Try a bigger number.")
elif guess > secret:
print("Too high! Try a smaller number.")
else:
print("đ You got it! The number was", secret)
Your guess: 10 Too high! Try a smaller number. Your guess: 5 Too low! Try a bigger number. Your guess: 7 Too low! Try a bigger number. Your guess: 8 đ You got it! The number was 8
Let's keep score! We make a variable called guesses that starts at 0, and add 1 to it every time the loop runs. When the loop finishes, the player has found the number, so we print how many tries it took. Try to beat your best score!
guesses = 0
# ...inside the while loop, after input():
guesses = guesses + 1
# ...after the loop has finished:
print("You needed", guesses, "guesses.")
Here is the whole program with every step put together. If something isn't working in your version, compare it with this one line by line. Check the spelling, the colons at the end of if and while lines, and that the indentation (the spaces at the start of lines) matches.
import random
print("đ˛ Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 20.")
secret = random.randint(1, 20)
guesses = 0
guess = 0
while guess != secret:
guess = int(input("Your guess: "))
guesses = guesses + 1
if guess < secret:
print("Too low! Try a bigger number.")
elif guess > secret:
print("Too high! Try a smaller number.")
else:
print("đ You got it! The number was", secret)
print("You needed", guesses, "guesses.")
This is what it looks like when someone plays. The words after each question are what the player typed.
đ˛ Welcome to the Number Guessing Game! I'm thinking of a number between 1 and 20. Your guess: 10 Too high! Try a smaller number. Your guess: 5 Too low! Try a bigger number. Your guess: 7 Too low! Try a bigger number. Your guess: 8 đ You got it! The number was 8 You needed 4 guesses.
Finished? Brilliant! Real programmers are always improving their programs. Try one of these challenges to make the project truly yours.
Stuck on a step? These lessons explain the Python ideas used in this project.