Skip to main content
🔒 Preview mode. The first 15 Foundations lessons are free; this one is Pro. Start a 7-day trial to unlock the editor, AI hints and the rest of the curriculum. Card required, cancel any time in Dashboard.Start 7-day trial →
← CoursesPython FoundationsModule 4 · Object-oriented programmingtry/except — catch an errorwrite56 / 174
+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`
✏️ Write your code here
🐍
Loading Python...
One-time download (~1 MB). Then it runs instantly.
📊 Result
Press Run to see result...

💬 Discussion

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