[Tutor] how to accept an integer?

Kent Johnson kent37 at tds.net
Wed Dec 5 22:58:33 CET 2007


Bryan Fodness wrote:
>     speed = int(raw_input(prompt))
> 
>  
> Is this how ALL known integers should be input?

Yes, with probably a try/except block and a while loop around it to 
handle invalid input.

There are two good reasons for doing this instead of using input:
- it guarantees that the input is in fact an integer (raises ValueError 
otherwise)
- it protects against malicious input (you can do a lot of damage in an 
input(), whatever you type is eval'ed)

For input of unknown integers, you are on your own, Python does not have 
anything built-in for that ;-)

Kent


More information about the Tutor mailing list