Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

int(some_float) vs. int(input(some_float))

When you do this:

int(3.0)
>>> 3

But when you do this:

int(input("Enter some float: ") # As the user I'll enter 3.0 again
>>>ValueError: invalid literal for int() with base 10: '3.0'

What's going on!?!? Thanks :)

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

int works on floats and strings that are integers. It does not work on strings that are floats.

>>> int(3.0)
3
>>> int("3.0")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '3.0'
>>> float("3.0")
3.0
>>> int(float("3.0"))
3
>>> 

Since you're asking for a float use float() first, then convert to an int.

Thanks so much!!