python catch int conversion error

Solutions on MaxInterview for python catch int conversion error by the best coders in the world

showing results for - "python catch int conversion error"
Ivanna
05 Jun 2017
1>>> for value in '12345', 67890, 3.14, 42, 0b010101, 0xFE, 'Not convertible':
2...     try:
3...         print('%s as an int is %d' % (str(value), int(value)))
4...     except ValueError as ex:
5...         print('"%s" cannot be converted to an int: %s' % (value, ex))
6...
712345 as an int is 12345
867890 as an int is 67890
93.14 as an int is 3
1042 as an int is 42
1121 as an int is 21
12254 as an int is 254
13"Not convertible" cannot be converted to an int: invalid literal for int() with base 10: 'Not convertible'
14