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 programmingCareless number parsingwrite60 / 174
+100 XP
Task
📝 **Task:** `parse_int(s)` returns the int or `-1` if parsing fails. 📋 **Steps:** 1. `def parse_int(s):` 2. `try: return int(s)` 3. `except ValueError: return -1` 💡 **Similar example (float):** ```python def to_float(s, default=0.0): try: return float(s) except ValueError: return default print(to_float("3.14")) # 3.14 print(to_float("abc")) # 0.0 ``` 🎯 **Expected output:** `42\n-1`
✏️ 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…