Can Python Guess Your Number?

Python Number Guesser Header

Last year we created a High Low Guessing Game as part of our series on Super Simple Python projects. In that game, the computer picks a number and then you guess the numbers. With each guess, the computer will tell you if the number you guessed is higher or lower than the number it picked.

In this game, we’re going to do the opposite. You, the player, are going to think of a number between 1 and 100 and the computer is going to guess your number. With each guess our Python program makes, you will tell it if its guess is higher, lower, or just right.

We’ve split this program into two pieces. In this post, we’ll cover:

  • Setting the First “Guess”
  • Having Python Guess Your Number
  • Summary of How Python Can Guess Your Number

Setting the First Guess

The first thing we need to do is set up our first guess. To have the computer guess our number, we’ll need the random library. Specifically, we’ll need the randint function from the random library. This function generates a random integer between the low and high values passed to it, inclusive of both of those values. 

We start off our program by prompting the player (you) to think of a number between 1 and 100. Next, we have the computer guess a random integer between 1 and 100. We set l and h as the lower and upper (higher) bounds of our guessing range. This will come in handy shortly. As the first step in our game, we’ll ask the player if the first randomly generated number is higher, lower, or on point.

from random import randint
print("Think of a number between 1 and 100")
l = 1
h = 100
x = randint(l, h)
guessed = input(f"Is {x} higher, lower, or did we guess on point?(h, l, y) ")

Having Python Guess Your Number

At this point, we’ve gotten it so that the Python program can guess a number. Now, to have the computer guess your number, we just have to repeat this process with changing high/low bounds.

We’ll do this with a while loop. Our loop will run while our number has not been guessed by the player. As long as the answer to the input isn’t y, we adjust our high/low bounds and guess again. If the number that Python guessed was lower, then we set the new low bound to the previous guess plus 1. Else if the number that Python guessed was higher, we set the new high bound to the previous guess minus 1.

Once we’ve reset the new bounds, we generate a new random integer and ask the user if that is their number.

while guessed != "y":
   if guessed == "l":
       l = x + 1
   elif guessed == "h":
       h = x - 1
   x = randint(l, h)
   guessed = input(f"How about {x}?(h, l, y) ")

The two screenshots below show what it would be like to play the game if we were thinking of the numbers 50 and 12, respectively. 

Computer Guesses the Number 50
Computer Guesses the Number 12

Summary of How Python Can Guess Your Number

In this post we learned how to create a simple Python program to guess a number you’re thinking of between 1 and 100. We used the randint function from the random library to generate random integers between a high and low bound. To kick off our guessing game, we present you, the player, with a number and ask you to tell the computer if it’s higher than, lower than, or exactly the number you have in your head.

Then, depending on your answer, the computer adjusts the high and low bounds. After adjusting the bounds, it then comes up with another guess and presents you with that. This goes on until the computer has guessed your number. 

BONUS – what’s the expected number of times that the computer has to guess before it guesses your number?

Further Reading

I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.

Yujian Tang

Leave a Reply

%d bloggers like this: