Club.noww.in
A guess the number game in Python needs three things: a secret from random.randint(1, 100), a loop that reads guesses with input(), and “too high” or “too low” hints until the player gets it:
secret = random.randint(1, 100) while (guess := int(input("Guess: "))) != secret: print("Too low" if guess < secret else "Too high")I played each version below in Command Prompt on Python 3.12.5, typing the guesses myself, including some bad input.
You’ll build the classic game with input checks, a version with limited guesses, and a twist where the computer guesses your number using binary search.
Guess the number game in Python (full code)
The game picks a number, then loops until you guess it, counting your tries. isdigit() rejects anything that isn’t a whole number, so typing a word doesn’t crash it:
import random random.seed(2026) # fixed seed so this demo is repeatable; remove it to play for real secret = random.randint(1, 100) # 1 and 100 are both possible tries = 0 print("I'm thinking of a number from 1 to 100.") while True: text = input("Your guess: ").strip() if not text.isdigit(): print(" Please enter a whole number.") continue guess = int(text) tries += 1 if guess < secret: print(" Too low.") elif guess > secret: print(" Too high.") else: print(f" You got it! The number was {secret}. Tries: {tries}") breakOutput:
I'm thinking of a number from 1 to 100. Your guess: 50 Too high. Your guess: abc Please enter a whole number. Your guess: 25 Too high. Your guess: 12 Too low. Your guess: 18 Too high. Your guess: 16 You got it! The number was 16. Tries: 5
“abc” gets a friendly message and doesn’t count as a try. The number was found on the fifth guess.The random.seed(2026) line only makes this demo repeatable; delete it and you’ll get a new number each game. More on random numbers in generating a random number in Python.
Adding a limit on guesses
To make it harder, give the player a fixed number of tries with a for loop. Returning True or False makes it easy to keep score across rounds:
import random def play(max_tries=7, low=1, high=100): secret = random.randint(low, high) for attempt in range(1, max_tries + 1): guess = int(input(f"Guess {attempt}/{max_tries}: ")) if guess == secret: print(" Correct!") return True print(" Higher." if guess < secret else " Lower.") print(f" Out of guesses. It was {secret}.") return False random.seed(7) play(max_tries=3)Output:
Guess 1/3: 50 Lower. Guess 2/3: 25 Higher. Guess 3/3: 37 Higher. Out of guesses. It was 42.
Three tries weren’t enough this time, so the game reveals the number.randint() includes both ends, unlike range(). That off-by-one difference catches people; see for i in range in Python.
Can the computer win the guess the number game in 7 tries?
If you always guess the middle of what’s left, you halve the possibilities each time. That’s binary search, and it never needs more than 7 guesses for 1 to 100:
def computer_guesses(secret, low=1, high=100): """The computer finds your number by always guessing the middle.""" tries = [] while True: guess = (low + high) // 2 tries.append(guess) if guess == secret: return tries if guess < secret: low = guess + 1 else: high = guess - 1 print("finding 73:", computer_guesses(73)) print("finding 1: ", computer_guesses(1)) worst = max(len(computer_guesses(n)) for n in range(1, 101)) print("most guesses ever needed for 1-100:", worst)Output:
finding 73: [50, 75, 62, 68, 71, 73] finding 1: [50, 25, 12, 6, 3, 1] most guesses ever needed for 1-100: 7
7 guesses cover up to 127 numbers, since 2 to the 7th power is 128.That makes 7 a fair limit for the 1-100 game: a smart player can always win. Binary search is covered properly in binary search in Python.
Ideas to extend the game
- Ask “Play again? (y/n)” and wrap the game in an outer loop.
- Let the player choose easy (1-50), medium (1-100) or hard (1-1000).
- Save the best score to a file with json.
- Say “warm” or “cold” based on how close the guess is.
Try these next if you’re building small Python games:
Frequently asked questions
How do you make a guess the number game in Python?
Pick a secret with random.randint(1, 100), then loop: read a guess with input(), convert it with int(), and print too high or too low until it matches.
How do I stop the game crashing on letters?
Check text.isdigit() before converting, or wrap int() in a try/except ValueError.
Does randint include both numbers?
Yes. random.randint(1, 100) can return 1 and 100.
How many guesses should I allow?
7 for 1 to 100, since binary search always finds the number in 7 guesses or fewer.
How do I add a play again option?
Put the game in a function and call it inside a while loop that asks the player whether to continue.
Where is random.randint documented?
See random.randint in the Python docs.












English (US)