Back

Activity 1.14: Number Guessing Game

divider

Activity 1.14

Number Guessing Game

Key Concepts

Sentinel-Controlled Loops

Looping Until the Right Answer

Not every loop counts a fixed number of times — some keep going until a specific value shows up. That value is called a sentinel.

score = 0
while score != -1:
score = int(input("Enter a score (-1 to stop):"))
# remaining code omitted

Today's Project

The computer picks a secret number from 1 to 10. The player keeps guessing — too high, too low — until they land on it.

Today's Objectives

  • Looping until the player guesses correctly
  • Giving feedback with an elif chain each pass through the loop
  • Counting how many tries it took

Key Terms

Sentinel Value
A specific value a loop watches for to know when to stop.

'F' → Fullscreen

divider

Build

Create a new Python program named 1-14-number-guessing-game.


Task 1: Set Up the Game

  • Pick a random secret number from 1 to 10, and set up a guess and a tries counter.
Number Guessing Game
import random
secret_number = random.randint(1, 10)
guess = 0
tries = 0

Task 2: The Guessing Loop

  • Loop until the guess matches the secret number, giving too-high/too-low feedback.
Number Guessing Game
import random
secret_number = random.randint(1, 10)
guess = 0
tries = 0
while guess != secret_number:
guess = int(input("Guess the number (1-10):"))
tries = tries + 1
if guess == secret_number:
print(f"Correct! It took you {tries} tries!")
elif guess > secret_number:
print("Wrong! Your guess was too high.")
elif guess < secret_number:
print("Wrong! Your guess was too low.")

Task 3: Handle Out-of-Range Guesses

  • Add a check for guesses outside 1-10, before the too-high/too-low checks.
Number Guessing Game
import random
secret_number = random.randint(1, 10)
guess = 0
tries = 0
while guess != secret_number:
guess = int(input("Guess the number (1-10):"))
tries = tries + 1
if guess == secret_number:
print(f"Correct! It took you {tries} tries!")
elif guess < 1 or guess > 10:
print("Your guess was out of range.")
elif guess > secret_number:
print("Wrong! Your guess was too high.")
elif guess < secret_number:
print("Wrong! Your guess was too low.")
divider

Checkpoint

Verify your program works correctly.

Example Output
Guess the number (1-10): 6 [Enter]
Wrong! Your guess was too high.
Guess the number (1-10): 3 [Enter]
Wrong! Your guess was too low.
Guess the number (1-10): 5 [Enter]
Wrong! Your guess was too high.
Guess the number (1-10): 4 [Enter]
Correct! It took you 4 tries!
divider

Reflection

Answer the following questions before submitting your work.

  1. What condition does the while loop check to decide whether to keep asking for another guess?
  2. Why does guess need to start at a value like 0, which could never actually be the secret number?
  3. Why didn't this activity need a final else branch to catch invalid input, the way the archive it's based on did?
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete