← Console bridge: text in, text out

Guess the number

Read a whole guessing game that uses loops, checks and a counter together, then change it and build a dice game from the same parts.

Real programs are longer than a few lines. The skill is to find the part that does a job, then change just that part.

This program uses only what you already know: input, while, a check, += and if / elif / else. It is longer, so the skill now is reading: finding which part does which job, and how the parts depend on each other.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

lesson.py
secret = 7
guess = input("Guess a number from 1 to 20: ")
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!")

Read the program, one part at a time. How many tries will it say?

The secret is 7. You type 10, then 3, then 7. What number does the last line show?

RunRun it and compare the result with your prediction.

Run

Run it with 10, 3, 7. The secret is fixed at 7 for now, so you can follow it. Watch for which part of the program runs after each guess.

lesson.py
secret = 7
guess = input("Guess a number from 1 to 20: ")
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!")
Try it yourselfYour edits are saved in this browser.

InvestigateTrace how each line of code connects to what you see.

Investigate

lesson.py
import random

secret = random.randint(1, 20)
guess = input("Guess a number from 1 to 20: ")
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!")

Match each part with its job.

The counter is always the same.

Match each part of the program with its job.

secret = random.randint(1, 20)
while guess != str(secret):
elif int(guess) < secret:
tries += 1

With this version the program always says Got it in 2 tries, however many guesses it took. Click the wrong line.

ModifyChange the code on purpose and check the result.

Modify

Change the game one step at a time. Run after each change.

Pick the secret from 1 to 100.

Need a hint?

Which line decides how big the secret can be?

Another hint

Change the second number in randint.

Stop after five wrong guesses and show the secret.

Need a hint?

The loop keeps going while the guess is wrong AND tries is under 5.

Another hint

After the loop, use an if to say either Got it or reveal the secret.

Try it yourselfYour edits are saved in this browser.

Pick the secret from 1 to 100.

    Run your program. This ticks when your code does all of the above.

    Allow only 5 tries, then reveal the secret.

      Run your program. This ticks when your code does all of the above.

      MakeBuild something new using what you have learned. Expect a few tries; that is normal.

      Make

      Make a dice game with the same parts. The computer rolls a dice (a random number from 1 to 6) again and again until it gets a six, then prints how many rolls it took.

      Try it yourselfYour edits are saved in this browser.
      Need a hint?

      Roll once before the loop. The loop keeps going while the roll is not 6.

      Another hint

      Inside the loop roll again and add 1 to the count.

      Roll a dice until a six comes up, then print how many rolls it took.

        Run your program. This ticks when your code does all of the above.

        For teachers

        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. About 35 minutes.

        Open the teacher guide: answers, solutions, slides and a worksheet.

        Students will be able to

        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.

        The hard part (on purpose)

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

        When students get stuck

        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.

        Where it leads

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

        What each step is for

        Now you can
        Next lesson9. A menu that keeps showingPut a menu inside while True, choose with if and elif, and leave the loop with break when the person picks quit.