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.
Answer
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
No comments:
Post a Comment