Python: How to check integer ?

Status
Not open for further replies.

nileshgr

Wise Old Owl
Hi I am new to Python.

Suppose an app is accepting input then how do I validate that it is integer or not ?
 

QwertyManiac

Commander in Chief
Use a simple try and catch block?

Code:
a = raw_input('Enter an integer: ')
try:
	a=int(a)
except:
	print 'Please enter a valid integer'
	exit(1)
print 'You entered:', a
 
OP
nileshgr

nileshgr

Wise Old Owl
Thanks. Well, my usage isn't command line but using mod_python & httpd. I need this for my new upcoming site which is going to be programmed in Python.
 

QwertyManiac

Commander in Chief
Well, basically all I'd said was to try an int() conversion and except its error messages in case it encounters a number/literal not suitable for conversion to base 10. That is how you check for an input being a valid integer convertible.

This method is not "command-line", only my test input was. Learn some more Python before jumping into the line of fire. :)
 
Status
Not open for further replies.
Top Bottom