Students learn to check typed text with isdigit before converting it, so a typo gets a reply, not a crash.
Console bridge: text in, text out · Numbers from text · about 30 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.
SlidesDownload Present from the browser, or export to PowerPoint or PDF.
WorksheetDownload Print the student copy; the Teacher edition has the answers.
Students will be able to
I can use isdigit to check typed text before turning it into a number.
I can say why the check goes before the conversion.
I can write a program that replies politely to a typo.
Timing
Predict and Run 8 min · Investigate 8 min · Modify 5 min · Mix it up 3 min · Make 8 min
Running the lesson
Predict: take a vote before running. The wrong options are three real beliefs: int reads words, the crash happens anyway, and nothing happens. Ask the 'error' voters what they think isdigit does, and the '13' voters whether Python can read words.
Run: run with 12, twelve and -3. Ask which path each took.
Investigate: the bug is about order. Ask a student to say what the program does first.
Make: students test with a number, zero and a word. Ask them to try something to break it (the empty answer, a space).
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.
Break my machine(end, 4 min)
Swap seats and try to crash your partner's ticket machine with unusual answers. Can you find one?
Empty answers, spaces, negative numbers and decimals are all good tries. Ask what the check says for each.
The hard part (on purpose)
Putting the conversion before the check. Ask which line runs first.
When students get stuck
What you will see
A question to ask
The fix
The program still crashes on a word.
Is int( ) inside the if, or before it?
Store the text first, check it, and convert only inside the if.
A NameError for age or number in the else path.
Where is that name made, and does the else path reach it?
Names made inside the if exist only on that path. The else path should only use the text.
Answers to the checks
Predict: You run it and type twelve. What happens? It prints Please type your age as a number. The if asks whether the text is all digits first. twelve is not, so the else path runs, and int is never reached.
Trace the check: For each answer typed, fill in what text.isdigit() gives and what the program prints. False, Please type your age as a number., True, Next year you will be 10., False, Please type your age as a number. isdigit() is True only when every character is a digit from 0 to 9. A full stop is not a digit, so 4.5 fails too.
Spot the bug: The program has a check, but typing twelve still crashes it. Click the line that is in the wrong place. line 2 int(text) runs on line 2, before the check on line 3, so the crash happens before the program has asked. The conversion has to go inside the if.
Mix it up: The person types 0. What does the program print? That will be $0. isdigit is True for 0, so the program calculates 0 tickets at $12, which is $0. The check only asks whether the text is all digits.
Solutions
One possible solution for each task. Other correct programs are fine.
Modify
first = input("First number: ")
second = input("Second number: ")
iffirst.isdigit() andsecond.isdigit():
total = int(first) + int(second)
print("The total is " + str(total) + ".")
else:
print("Please type two whole numbers.")
Make
text = input("How many tickets? ")
iftext.isdigit():
print("That will be $" + str(int(text) * 12) + ".")
else:
print("Please type a number, such as 3.")
Stretch and support
Stretch: What does the check say for an empty answer? Try it and fix the message.
Support: Give the if / else with the middle lines missing and ask the student to complete them.
Exit ticket
Why does the check go before int, and what does text.isdigit() give?
int stops the program on text that is not digits, so the program asks first. isdigit gives True or False.
What each step is for
Investigate: The check goes before the conversion. Asking first is what stops the crash.
Modify: Two checks joined with and: the if runs only when both are true.
Mix it up: isdigit checks the shape of the text, not whether the answer makes sense.
Where it leads
Next lesson: instead of ending after one bad answer, the program keeps asking until it gets a good one.