Catch an error with try and except, so a typing mistake gets a helpful reply and the program asks again.
Apps you trust never crash when you mistype: they catch the mistake, say what they wanted, and ask again.
In the console bridge you checked typed numbers with isdigit() before using them. Another way is to just try, and say what to do if it goes wrong. try runs some lines; if one of them causes a ValueError, Python jumps to except ValueError: instead of stopping the program.
Make your prediction first. Help opens once you've locked it in.
text = "twelve"
try:
age = int(text)
print("Next year: " + str(age + 1))
except ValueError:
print("That is not a number.")
print("Done")
int("twelve") cannot work. Inside try, what happens to the next line, and does the program stop?
Try three, then 3.5, then 4. The loop asks again after each mistake; break leaves it once int() works.
while True:
text = input("How many tickets? ")
try:
tickets = int(text)
break
except ValueError:
print("Please type a whole number, such as 3.")
print("That will be $" + str(tickets * 12) + ".")
while True:
text = input("How many tickets? ")
try:
tickets = int(text)
break
except ValueError:
print("Please type a whole number, such as 3.")
print("That will be $" + str(tickets * 12) + ".")
What int() accepts.
Two ways to deal with bad input.
An error that was not caught.
This should say There is no such room, but it stops with KeyError: attic. Click the line that is wrong.
A canteen with two kinds of mistake: an item that is not on the menu (KeyError) and a number that is not a number (ValueError). Try both.
Only accept 1 to 10 of an item.
After count = int(text), only break if the count is between 1 and 10.
if count >= 1 and count <= 10: then break; otherwise print Choose 1 to 10.
Let the customer type quit instead of an item.
Before the try in the first loop, add: if item == "quit": break, and say goodbye instead of asking how many.
Only accept 1 to 10 of an item.
Run your program. This ticks when your code does all of the above.
Let the customer type quit instead of an item.
Run your program. This ticks when your code does all of the above.
Ask for two whole numbers and print the first divided by the second. Catch two kinds of mistake, each with its own except and its own message: a typing mistake (ValueError) and dividing by zero (ZeroDivisionError). After either, ask again.
Divide two numbers, catching ValueError and ZeroDivisionError, and ask again after either.
Run your program. This ticks when your code does all of the above.
Next: the text adventure, built from the parser, the bag, the map and safe input.
Students catch errors with try and except so typed mistakes get a reply instead of a crash, compare it with checking first (isdigit), and catch two kinds of error in one program. About 45 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
Read the unit primer first. try: runs its lines; if one causes the named error, Python jumps to except ThatError: and then carries on after it. Only the named error is caught: the bug task catches ValueError when the error is a KeyError, so the program still stops. int() accepts " 4 " and "-2" but not "3.5" or "three", so a caught error does not check whether a number is sensible (the first Investigate question). Avoid a bare except: that catches everything: it hides real mistakes.
Catching the error that actually happens. Ask them to read the error's name in the message and match it to the except, rather than catching everything.
Stretch: Make a function ask_number(question) that keeps asking until it gets a whole number, and use it twice.
Support: Give students the try and the first except of the calculator, and ask them to add the second except.
Next lesson is the text adventure. Later, saving and loading a game uses the same idea for a missing file (FileNotFoundError).