Search⌘ K

Try Out Things Safely!

Explore how to handle errors in Python using try and except statements. Learn to catch exceptions like ValueError, prevent crashes, and create programs that respond gracefully when unexpected input occurs.

Our code can now make decisions, but what happens when something goes wrong?
In this lesson, we’ll learn how to use try and except to handle errors and keep our programs from crashing.


Break something (on purpose)

Try running this:

number = int(input("Enter a number: "))
print("Half of your number is", number / 2)
Asking for a number and dividing it by 2

Now run it again, but instead of entering ...