Lesson 8.1
A program crashes when it meets an error. You can catch it with the try and except
blocks:
try:
number = int(raw)
except:
print("That is not a number")
try runs the risky code: int(raw) fails if raw is text like hello.
except runs if an error happened, and the program does not crash.
Keep only the risky line inside try.
The check puts into raw whatever the user typed. Do two things:
raw is a number, number gets it;Entered: <number>.On an error number stays None, and the program prints Entered: None.
Remember: an except without a type catches everything. You'll learn to catch a
specific error in the next lesson.
number is needed outside try, so it can be printed after an error.
The risky line is only int(raw), the rest goes outside try. In except — a print
with the hint. At the end one shared print with the label Entered:.