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.
Predict: Read the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.
Run: Run it and compare the result with your prediction.
Investigate: Trace how each line of code connects to what you see.
Modify: Change the code on purpose and check the result.
Make: Build something new using what you have learned. Expect a few tries; that is normal.
Make your prediction first. Help opens once you've locked it in.
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 = 1while guess != str(secret):
ifguess.isdigit() == False:
print("Type a whole number.")
elifint(guess) < secret:
print("Too low.")
else:
print("Too high.")
guess = input("Try again: ")
tries += 1print("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 = 1while guess != str(secret):
ifguess.isdigit() == False:
print("Type a whole number.")
elifint(guess) < secret:
print("Too low.")
else:
print("Too high.")
guess = input("Try again: ")
tries += 1print("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 = 1while guess != str(secret):
ifguess.isdigit() == False:
print("Type a whole number.")
elifint(guess) < secret:
print("Too low.")
else:
print("Too high.")
guess = input("Try again: ")
tries += 1print("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.
I can point to the lines that do each job in a longer program.
I can change one part of a program without breaking the rest.
I can build a new loop from parts I know.
Running the lesson
Predict: students read the program and work out the count by hand before running.
Run: run with 10, 3, 7. Ask which line ended the loop.
Investigate: the match task is the key. Ask which part would you change to make the game harder?
Modify: encourage one change and a run at a time, and ask what each change affected.
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
The loop ends at once or never. Ask: What does the condition say, and what changes it? Fix: Trace the first two passes with the typed answers.
A TypeError comparing text with a number. Ask: Which side is text and which is a number? Fix: guess is text. Use int(guess) to compare it with the secret, or str(secret) to compare as text.
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
Investigate: A longer program is made of parts that each do one job. Find the part, see what it depends on, and you can change it without breaking the rest.
Modify: Change one part at a time and run after each change; the rest of the program should keep working.
Now you can
Point to the lines that do each job in a longer program.
Change one part of a program without breaking the rest.