safe_int(text) that converts a string text to an int and returns it. If the conversion fails (the string is not a valid integer), return None instead of raising an error.safe_int("42")42
Valid integer string.
safe_int("abc")None
"abc" cannot become an int.
Wrap `int(text)` in a try/except ValueError.
Remember int() already strips surrounding whitespace on valid numbers.