[Tutor] why does this raise an exception...

Tim Wilson wilson at visi.com
Thu May 13 09:57:41 EDT 2004

On 5/13/04 8:55 AM, "roman" <rcher at inter.net.il> wrote:

> When converting a few string values to integers with int() I found out
> that trying to convert "2.1", i.e. a string that represents a floating
> point value to an integer raises an exception. Why does this raise an
> exception while something like float("9") does not?

Since 9 = 9.0, I wouldn't expect float("9") to raise an exception. But what
should the computer do with 2.1 when converting to an int? I don't think I
want my code changing 2.1 to 2, for example, without letting me know. You
can also round your float and then convert to int, if that's the behavior
you're looking for.


Tim Wilson
Twin Cities, Minnesota, USA
Educational technology guy, Linux and OS X fan, Grad. student, Daddy
mailto: wilson at visi.com   aim: tis270   public key: 0x8C0F8813

More information about the Tutor mailing list