Saturday, August 13, 2011

invalid literal for int() with base 10:

So I'm learning Python programming in my spare time.
For two hours I was stuck on this simple error:
invalid literal for int() with base 10:

Turns out, if you input a string number like "4.0" and you want the integer of it,
you must first make the string a float, and then make the float an integer.
"4.0" > float("4.0") > int(4.0) is the path.

So
>>>int("4.0") #this will give you an error
>>>int(float("4.0")) #this will work and give you >>>4

The reason I think this is necessary is because Python wants you to verify that you
know you are converting a float to an integer. It doesn't want to assume you know that.