Skip to main content
← CoursesPython FoundationsModule 4 · Object-oriented programmingCareless number parsingwrite57 / 170
💬 Discuss🧪 Playground+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`

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…