Ask the typed text whether it is all digits with isdigit, and use an if to choose, so a typo gets an answer instead of a crash.
Type a word into a quantity box and a good app says so politely. A careless one crashes. The difference is one question asked before the number is used.
Last lesson int() turned typed digits into a number. But int("twelve") stops the program with an error. A careful program checks first, and chooses what to do.
Make your prediction first. Help opens once you've locked it in.
text = input("How old are you? ")
if text.isdigit():
age = int(text)
print("Next year you will be " + str(age + 1) + ".")
else:
print("Please type your age as a number.")
You know if and else. Read the code, including the new text.isdigit(). What happens if you type twelve?
Run it three times: with 12, with twelve, and with -3.
| Line | What it does | Why |
|---|---|---|
if text.isdigit(): |
Asks the text: are you made only of digits? | The dot means "ask this text". The answer is True or False, which an if can use |
age = int(text) |
Turns the text into a number | Only runs when the check said yes, so it cannot fail |
else: |
Runs when the check said no | The person gets a reply instead of a crash |
A minus sign is not a digit, so -3 fails the check. That suits an age.
text = input("How old are you? ")
if text.isdigit():
age = int(text)
print("Next year you will be " + str(age + 1) + ".")
else:
print("Please type your age as a number.")
text = input("How old are you? ")
if text.isdigit():
age = int(text)
print("Next year you will be " + str(age + 1) + ".")
else:
print("Please type your age as a number.")
Four answers, four paths.
The check is there, but it comes too late.
For each answer typed, fill in what text.isdigit() gives and what the program prints.
| typed | text.isdigit() | prints |
|---|---|---|
| 12 | True | Next year you will be 13. |
| twelve | ||
| 9 | ||
| 4.5 |
The program has a check, but typing twelve still crashes it. Click the line that is in the wrong place.
Change the program so it asks for two numbers, checks both, and prints their total. If either answer is not a number, print a message instead.
Ask for two answers, store them as text, and check both: first.isdigit() and second.isdigit().
Inside the if, convert each with int and print the total.
Ask for two numbers, check both, and print their total.
Run your program. This ticks when your code does all of the above.
This mixes the last lesson and if: a typed number, converted, and used.
text = input("How many tickets? ")
if text.isdigit():
print("That will be $" + str(int(text) * 12) + ".")
else:
print("Please type a number.")
Tickets to the school play cost $12 each. Write a ticket machine that asks how many tickets, checks the answer is a number, and prints the total, such as That will be $36. If the answer is not a number, it prints a helpful message. Test it with 3, with 0 and with three.
Store the answer as text first, then check it, then convert it inside the if.
Put the cost calculation inside the if, and a helpful message in the else.
Ask how many tickets, check it with isdigit, and print the total cost.
Run your program. This ticks when your code does all of the above.
Students learn to check typed text with isdigit before converting it, so a typo gets a reply, not a crash. About 30 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
Putting the conversion before the check. Ask which line runs first.
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.
Next lesson: instead of ending after one bad answer, the program keeps asking until it gets a good one.