I want a program to convert user input to an integer and store it in a list only if it can be. If the input is not able to be converted to an integer, the program should just continue and ignore the input.
Asked
Active
Viewed 261 times
0
-
Note that there are special cases like `1e+2` etc that can be converted to int – John La Rooy May 20 '14 at 23:08
1 Answers
3
The best approach here is to just try and convert it and ignore the error if the conversion fails. For example:
try:
your_list.append(int(user_input))
except ValueError:
pass

Andrew Clark
- 202,379
- 35
- 273
- 306