Editor

Safe Division

Beginner · Write the code · from Python Try-Except: Handle Errors Without Crashing

From the lesson: Python Try-Except: Handle Errors Without Crashing

Imagine a trapeze artist performing high above the ground. No matter how skilled they are, there's always a safety net below. If they slip, the net catches them and they can climb back up and try again.

Read the lesson

Write a function called safe_divide(a, b) that returns the result of a / b. If b is zero, it should catch the ZeroDivisionError and return the string "Cannot divide by zero" instead.

Print the result of calling safe_divide(10, 2) and then safe_divide(10, 0).

# Write your safe_divide function here

More Try-Except exercises

All Python exercises · 50 practice problems · Error Handling quiz