# Can Python Guess Your Number?

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.

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.