Editor

Python Program for a Number Guessing Game

Beginner · Games and random numbers

In the number guessing game the computer picks a secret number from 1 to 100, and the player keeps guessing while the computer answers "too high" or "too low" until the guess is right. Here the program plays the player too, using the smartest strategy of always guessing the middle of the numbers that are still possible, so it runs without anyone typing.

Program

number_guessing_game.py
import random
secret = random.randint(1, 100)
print("I am thinking of a number from 1 to 100.")
# The player guesses the middle of the range that is still possible.
# A human player would type each guess instead:
# guess = int(input("Your guess: "))
low = 1
high = 100
attempts = 0
while True:
guess = (low + high) // 2
attempts += 1
if guess < secret:
print(f"Guess {attempts}: {guess} is too low")
low = guess + 1
elif guess > secret:
print(f"Guess {attempts}: {guess} is too high")
high = guess - 1
else:
print(f"Guess {attempts}: {guess} is correct!")
break
print("Found the number", secret, "in", attempts, "guesses")
Output
I am thinking of a number from 1 to 100.
Guess 1: 50 is too high
Guess 2: 25 is too low
Guess 3: 37 is too low
Guess 4: 43 is correct!
Found the number 43 in 4 guesses

How it works

  1. random.randint(1, 100) picks the secret number, so it is different on every run, and so is the output.
  2. Each guess is the middle of the range from low to high, worked out with whole number division //.
  3. When the guess is too low, every number up to it is ruled out, so low moves to guess + 1; when it is too high, high moves to guess - 1.
  4. Halving the range every time means the number is always found in at most 7 guesses, which is why the loop is sure to end with break.
  5. To play the game yourself, replace the line that works out guess with the input() line from the comment.

Another way to write it

With a limited number of chances

With a limited number of chances
secret = 42
guesses = [50, 25, 37, 43, 40] # what a player might type, one per chance
chances = 5
for turn in range(chances):
guess = guesses[turn] # or: guess = int(input("Your guess: "))
if guess == secret:
print("Correct! You got it in", turn + 1, "tries")
break
elif guess < secret:
print(guess, "is too low. Chances left:", chances - turn - 1)
else:
print(guess, "is too high. Chances left:", chances - turn - 1)
else:
print("Out of chances! The number was", secret)
Output
50 is too high. Chances left: 4
25 is too low. Chances left: 3
37 is too low. Chances left: 2
43 is too high. Chances left: 1
40 is too low. Chances left: 0
Out of chances! The number was 42

Here the secret and the guesses are fixed, so the output is the same on every run and shows the losing case. The else after the for loop runs only when the loop ends without break, that is, when no guess was right.

Try it yourself

Change the range to 1 to 1000 and check that the program still finds the number in at most 10 guesses.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises