Search⌘ K
AI Features

Challenge: Safe Division

Explore how to handle errors in Python by writing a program that safely divides two user-provided numbers. Learn to use try except blocks to prevent crashes from invalid inputs and display friendly error messages, helping you build reliable and user-friendly code.

We'll cover the following...

Sometimes programs crash when something unexpected happens, for example:

  • dividing by zero

  • typing letters instead of numbers

Python gives us a way to try running code, and handle errors if something goes wrong.

This is done using a try / except block.

Your task:

  1. Ask the user for two numbers

  2. Try to divide the first number by the second

  3. If the division works, print the result

  4. If any error happens, print a friendly message instead of crashing

💡 How this works:

  • Code inside try is run normally

  • If an error happens, Python jumps to except

  • The program continues instead of stopping

You don’t need to handle specific errors yet, one general error message is enough.

When you click Run:

  • Entering valid numbers should show the result

  • Entering something invalid should show your friendly message

# Write your code here.
Safely dividing two numbers and handling division by zero and invalid input