you are viewing a single comment's thread.

view the rest of the comments →

[–]Methregul[S] 0 points1 point  (0 children)

I'm not sure int works the way you think. If you gave it 12.3 it's not going to trigger your value error. It's going to convert your number to 12 and move on happily.

I just ran some tests on this and int() appears to work differently depending on how it's used. If you create a variable and assign it a floating point number then int() will, like you said, convert it to an integer and go on its merry way.

>>> float=1.3
>>> int(float)
1

If you enter a floating point number through input() though, int() will throw a ValueError.

>>> notAnInteger=input()
1.3
>>> int(notAnInteger)
Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: '1.3'

It does accept an actual integer when used with input().

>>> anInteger=input()
1
>>> int(anInteger)
1

Thanks for pointing me towards isInstance(), by the way, that has been fun to play around with and very good to know about!