# Python: How to check integer ?



## nileshgr (Jan 27, 2008)

Hi I am new to Python.

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


----------



## QwertyManiac (Jan 27, 2008)

Use a simple try and catch block?


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


----------



## nileshgr (Jan 27, 2008)

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 (Jan 27, 2008)

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.


----------

