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:
Ask the user for two numbers
Try to divide the first number by the second
If the division works, print the result
If any error happens, print a friendly message instead of crashing
💡 How this works:
Code inside
tryis run normallyIf an error happens, Python jumps to
exceptThe 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.