(9/10)

Handling Errors (Exception Handling) — Python typing lesson

Learn try/except to handle errors gracefully.

What you'll practice

What if a user types text when we expect a number? Our program could crash! Let's learn to handle errors with `try` and `except`.

  • Python will `try` to run the code inside the `try` block.
  • If an error happens in the `try` block, the code in the `except` block will run instead.

Pro Access Required

This is a premium lesson. Upgrade to Pro to unlock this interactive exercise and continue your learning journey.

🐍Python

Handling Errors (Exception Handling)

0 WPM
0% Accuracy
0:00 Time elapsed
1
2
3
4
5
6
7
number_as_text = input('Give me a number: ')↵
↵
try:↵
number = int(number_as_text)↵
print('Your number doubled is ' + str(number * 2))↵
except:↵
print('Whoops! That was not a number.')
0 / 190
Press This!
👇
`~
1!
2@
3#
4$
5%
6^
7&
8*
9(
0)
-_
=+
⌫
⇥
Q
W
E
R
T
Y
U
I
O
P
[{
]}
\|
⇪
A
S
D
F
G
H
J
K
L
;:
'"
↵
⇧
Z
X
C
V
B
N
M
,<
.>
/?
⇧
Thumb
Index
Middle
Ring
Pinky