PYTHON CORE · LESSON 34

When the mistake happened.

try and except · about 13 minutes

Learning with progressRead the summary for free. To save the work, log in to the account.
STEP 1

Try and do it.

In try put an action that can end in error. For example, a word cannot be turned into a number.

try:
    number = int("кот")
What happens when you make a mistake
trytryMistakenumberlessexceptexplanation

✦ Without processing there would be an error.

STEP 2

Help the program continue

except ValueError will work if the text is not converted to a number.

try:
    number = int("кот")
except ValueError:
    print("Это не число")

✦ Now the program will explain the problem calmly.

IN-CLASS PRACTICE

Type the code yourself

Dial the error handling unit. Check the indentations.

try:
    number = int("abc")
except ValueError:
    print("Не число")
Open the editor for class work
HOMEWORK

Solve it yourself

Try turning the word «кот» into a number inside try. In except ValueError, output «Не число». Name the variable number.

The answer is hidden. Use similar examples from the lesson notes.

Opening up your homework