Objective
int() raises ValueError if the text is not an integer. A function can catch that error and return None to signal that the conversion was not possible.
Instructions
- Write
to_int(text)that returns the integer orNoneif it cannot be converted. - Read
nand thenntexts; for each one show'text': numberwith the result, or'text': invalidif it is not valid (use!rto show the text).
3
12
abc
-7
'12': 12
'abc': invalid
'-7': -7
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise