Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 27-01-2008, 12:46 PM   #1 (permalink)
Wise Old Owl
 
The Unknown's Avatar
 
Join Date: Nov 2006
Location: Pune, Maharashtra, India
Posts: 1,728
Default Python: How to check integer ?


Hi I am new to Python.

Suppose an app is accepting input then how do I validate that it is integer or not ?
__________________
KDE on ArchLinux
PHP, MySQL, PostgreSQL, Linux, Apache; Message me to hire (freelancing only)
Explore Technology @ http://www.itech7.com
Cheap and Reliable VPS Hosting @ http://j.mp/arHk5e
The Unknown is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 27-01-2008, 01:16 PM   #2 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Python: How to check integer ?

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
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 27-01-2008, 01:56 PM   #3 (permalink)
Wise Old Owl
 
The Unknown's Avatar
 
Join Date: Nov 2006
Location: Pune, Maharashtra, India
Posts: 1,728
Default Re: Python: How to check integer ?

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.
__________________
KDE on ArchLinux
PHP, MySQL, PostgreSQL, Linux, Apache; Message me to hire (freelancing only)
Explore Technology @ http://www.itech7.com
Cheap and Reliable VPS Hosting @ http://j.mp/arHk5e
The Unknown is offline  
Old 27-01-2008, 04:29 PM   #4 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Python: How to check integer ?

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.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python tutorial (this is a request) karmanya Programming 4 27-05-2007 05:48 AM
Python Programming The Unknown Programming 15 21-04-2007 09:57 PM
best site to learn python language knoahshyam Tutorials 2 19-02-2006 02:21 PM
Python Unplugged .. !! shrek_incredible Open Source 1 14-11-2004 12:09 PM

 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:49 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2