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.
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.
Predict and Run 8 min · Investigate 9 min · Modify 8 min · Make 10 min
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.
Read the program with your partner. One of you says each line aloud; the other keeps the value of tries on paper.
Count before anyone runs it. The common slip is forgetting tries starts at 1.
Holding the whole program in mind. Split it into jobs and trace one pass.
| What you will see | A question to ask | The 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. |
One possible solution for each task. Other correct programs are fine.
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!")
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) + ".")
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: 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.
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.
Next lesson: a menu that keeps showing until the person chooses to quit.