easy +10 pts

Safe Float Parser

Parse a string to a float without crashing on bad input.

Write a function `safe_float(value)` that attempts to convert the given `value` to a float. If the conversion succeeds, return the float. If the conversion fails (e.g., because the input is not a valid number, or is `None`), return `None`. The function should never raise an exception. The input will always be either a string or `None`, but your function should handle any type gracefully. **Function signature:** `def safe_float(value):`

Constraints

Input can be a string, None, or any other object. The function should never raise an exception and must always return either a float or None.

Example

>>> safe_float("3.14")
3.14
>>> safe_float("hello")
None
>>> safe_float(None)
None
>>> safe_float("1e3")
1000.0
10 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use a try/except block to catch conversion errors.
The exception to catch is `ValueError` and possibly `TypeError`.
If the conversion fails, return `None` in the except block.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.