01LearnForge

Library / Python

Build a 'Guess the Number' Game in Python

Learn Python fundamentals by building a classic 'Guess the Number' game. This lesson covers variables, input/output, random numbers, conditional logic, and loops.

by codewithfaith001·Beginner·2 min·0 remixes

Want your own version of this lesson, tailored to your level?

⑂ Generate my version

What this lesson teaches, step by step

01Set Up the Secret Number and Welcome Message
0:00
import random

secret_number = random.randint(1, 20)
print("Welcome to 'Guess the Number'!")
print("I'm thinking of a number between 1 and 20.")

We start by importing the 'random' module, which lets our program do unpredictable things, like picking a random number. Then, we use `random.randint(1, 20)` to generate a secret number between 1 and 20 (inclusive) and store it in a variable called `secret_number`. Finally, `print()` displays a friendly welcome message to the player.

02Allow Multiple Guesses with a Loop
4:00
import random

secret_number = random.randint(1, 20)
print("Welcome to 'Guess the Number'!")
print("I'm thinking of a number between 1 and 20.")

guesses_taken = 0
while guesses_taken < 6:
    print("Take a guess.")
    player_guess = int(input())
    guesses_taken += 1

To let the player guess multiple times, we introduce a `while` loop. This loop will continue as long as `guesses_taken` is less than 6. Inside the loop, `input()` asks the player for their guess, and `int()` converts their text input into a whole number. We also increase `guesses_taken` by 1 after each attempt.

03Check the Player's Guess
8:00
import random

secret_number = random.randint(1, 20)
print("Welcome to 'Guess the Number'!")
print("I'm thinking of a number between 1 and 20.")

guesses_taken = 0
while guesses_taken < 6:
    print("Take a guess.")
    player_guess = int(input())
    guesses_taken += 1

    if player_guess < secret_number:
        print("Your guess is too low.")
    elif player_guess > secret_number:
        print("Your guess is too high.")
    else:
        break

Now we add conditional logic using `if`, `elif` (else if), and `else` statements. These check if the player's guess is too low, too high, or exactly right. If the guess is correct, the `break` statement immediately stops the `while` loop, moving the program to the next part.

04Announce the Win
12:00
import random

secret_number = random.randint(1, 20)
print("Welcome to 'Guess the Number'!")
print("I'm thinking of a number between 1 and 20.")

guesses_taken = 0
while guesses_taken < 6:
    print("Take a guess.")
    player_guess = int(input())
    guesses_taken += 1

    if player_guess < secret_number:
        print("Your guess is too low.")
    elif player_guess > secret_number:
        print("Your guess is too high.")
    else:
        print(f"Good job! You guessed my number in {guesses_taken} guesses!")
        break

When the player guesses correctly, we want to tell them they won and how many tries it took. We modify the `else` block to print a success message. The `f-string` (the `f` before the quote) allows us to easily embed the value of `guesses_taken` directly into our output string.

05Handle Losing Condition and End Game
16:00
import random

secret_number = random.randint(1, 20)
print("Welcome to 'Guess the Number'!")
print("I'm thinking of a number between 1 and 20.")

guesses_taken = 0
while guesses_taken < 6:
    print("Take a guess.")
    player_guess = int(input())
    guesses_taken += 1

    if player_guess < secret_number:
        print("Your guess is too low.")
    elif player_guess > secret_number:
        print("Your guess is too high.")
    else:
        print(f"Good job! You guessed my number in {guesses_taken} guesses!")
        break

if player_guess != secret_number:
    print(f"Nope. The number I was thinking of was {secret_number}")

After the `while` loop finishes (either by guessing correctly or running out of guesses), we need to check if the player lost. If `player_guess` is still not equal to `secret_number`, it means the loop ended because they ran out of guesses. In that case, we print a message revealing the correct number, bringing the game to a close.

Comments

  • codewithfaith00119d ago

    nice