Python program: Number guessing game
The computer picks a number and the player guesses, with "too high" and "too low" hints.
The code
import random random.seed(3) secret = random.randint(1, 100) guesses = [50, 25, 37, 31, 34, 35] # in a real game: int(input("Guess: ")) for tries, guess in enumerate(guesses, start=1): if guess < secret: print(guess, "too low") elif guess > secret: print(guess, "too high") else: print(f"{guess} is right! {tries} guesses.") break
Output
50 too high 25 too low 37 too high 31 is right! 4 guesses.
How it works
A loop compares each guess with the secret number. In the playground, replace the list with input() and a while loop to play for real; the First Projects lesson builds the full game.
Python features used
Learn the ideas behind it in the free lesson Py-Projects.