Python - Check If Multiple String Entries Contain Invalid Chars
Using Python v2.x, I have 3 variables that I want to ask the user for, as below: def Main(): Class_A_Input = int(raw_input('Enter Class A tickets sold: ')) Class_B_Input =
Solution 1:
Calling int
on something that isn't a valid integer will raise a ValueError
exception. You can just catch that. Or is there some further restriction you want that goes beyond that?
Solution 2:
import sys
try:
Class_A_Input = int(raw_input('Enter Class A tickets sold: '))
except ValueError:
print "First input was not a number."
sys.exit(1)
Will this pattern work for your use case?
Solution 3:
Or you can omit the use of int(), and check the Class_A_input
using isdigit()
>>> "asdf1".isdigit()
False
>>> "123".isdigit()
True
Post a Comment for "Python - Check If Multiple String Entries Contain Invalid Chars"