[Tutor] Force a value to int

Kent Johnson kent37 at tds.net
Thu Apr 13 14:06:34 CEST 2006


Ed Singleton wrote:
> Is there a particular way to force a value to be an int by either
> converting it with int() or returning a default value.
> 
> I've ended up writing my own function to do it, but it seems like the
> kind of thing that would be built-in somewhere.

No, there is no built-in for this but it's easy to write your own:

def my_int(value, default):
   try:
     return int(value)
   except ValueError:
     return default

Kent



More information about the Tutor mailing list