Skip to main content
← CoursesPython FoundationsModule 4 · Object-oriented programmingtry/except — catch an errorwrite53 / 170
💬 Discuss🧪 Playground+75 XP
Task
📝 **Task:** `safe_divide(a, b)` returns `a / b` or `"Error"` on division by 0. 📋 **Steps:** 1. `def safe_divide(a, b):` 2. Wrap division in `try:` 3. Catch `ZeroDivisionError` in `except` 4. Return `"Error"` in the except branch 💡 **Similar example (sqrt):** ```python def safe_sqrt(x): try: return x ** 0.5 except TypeError: return "Error" print(safe_sqrt(16)) # 4.0 print(safe_sqrt("abc")) # "Error" ``` 🎯 **Expected output:** `5.0\nError`

Keep going

✏️ Write your code here
🐍
Loading Python...
First visit only — ~5-10s. Stays cached afterward.
📊 Result
Press Run to see result...
📣 Help someone learn PythonShare this lesson with a friend — the first 15 are free, no signup.Tweet

💬 Discussion

Be the first to ask a question or share a tip.
Sign in to join the discussion. Reading is free.
Loading discussion…