← Teacher guide

Guess the number

Students read a guessing game made of moves they know, find the part that does each job, change one part, and build a dice game from the same pieces.

Console bridge: text in, text out · Keep asking · about 35 minutes · Open the student lesson

Classroom resources

Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Run 8 min · Investigate 9 min · Modify 8 min · Make 10 min

Running the lesson

  1. Predict: students read the program and work out the count by hand before running.
  2. Run: run with 10, 3, 7. Ask which line ended the loop.
  3. Investigate: the match task is the key. Ask which part would you change to make the game harder?
  4. Modify: encourage one change and a run at a time, and ask what each change affected.
  5. Make: the dice game is the same shape as the guessing game with the check removed.

In class

On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.

The hard part (on purpose)

Holding the whole program in mind. Split it into jobs and trace one pass.

When students get stuck

What you will seeA question to askThe fix
The loop ends at once or never.What does the condition say, and what changes it?Trace the first two passes with the typed answers.
A TypeError comparing text with a number.Which side is text and which is a number?guess is text. Use int(guess) to compare it with the secret, or str(secret) to compare as text.

Answers to the checks

  1. Predict: The secret is 7. You type 10, then 3, then 7. What number does the last line show?
    3. tries starts at 1. The wrong guess 10 is followed by one more try, so tries is 2, then the wrong guess 3 makes it 3. 7 is right, so the loop ends and the program shows Got it in 3 tries!
  2. Find the parts: Match each part of the program with its job.
    secret = random.randint(1, 20) → D. the computer picks a secret number; while guess != str(secret): → B. keeps asking until the guess is the secret; elif int(guess) < secret: → C. tells the player the guess was too low; tries += 1 → A. counts every guess
    The loop is the engine: it repeats the check, the hint, the new question and the count until the guess is right.
  3. Spot the bug: With this version the program always says Got it in 2 tries, however many guesses it took. Click the wrong line.
    line 12
    tries += 1 is not indented, so it runs once after the loop, not on every guess. Indent it so it runs on every pass.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify › A bigger range

import random

secret = random.randint(1, 100)
guess = input("Guess a number from 1 to 100: ")
tries = 1
while guess != str(secret):
    if guess.isdigit() == False:
        print("Type a whole number.")
    elif int(guess) < secret:
        print("Too low.")
    else:
        print("Too high.")
    guess = input("Try again: ")
    tries += 1
print("Got it in " + str(tries) + " tries!")

Modify › Only five tries

import random

secret = random.randint(1, 20)
guess = input("Guess a number from 1 to 20: ")
tries = 1
while guess != str(secret) and tries < 5:
    if guess.isdigit() == False:
        print("Type a whole number.")
    elif int(guess) < secret:
        print("Too low.")
    else:
        print("Too high.")
    guess = input("Try again: ")
    tries += 1
if guess == str(secret):
    print("Got it in " + str(tries) + " tries!")
else:
    print("Out of tries. The secret was " + str(secret) + ".")

Make

import random

roll = random.randint(1, 6)
count = 1
while roll != 6:
    roll = random.randint(1, 6)
    count += 1
print("It took " + str(count) + " rolls to get a six.")

Stretch and support

Stretch: Add a 'warm' hint when the guess is within 3 of the secret.

Support: Give the dice program with the loop condition missing and ask for it.

Exit ticket

Which line in the guessing game makes it ask again, and which line counts the guesses?

guess = input("Try again: ") asks again, and tries += 1 counts.

What each step is for

Where it leads

Next lesson: a menu that keeps showing until the person chooses to quit.