Python Forum

Full Version: int() ValueError
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Very simple chunk of code:

ip = input("Enter #:")
ip = int(ip)
If I enter 2.3, why do I get:
ValueError: invalid literal for int() with base 10: '2.3'

If int() accepts a string or a float, why does it not handle this value by truncating?
First, after you enter a number (in your case 2.3), ip variable is a string, and you can't cast a "float string" to integer.
ip = int(float(ip)) would work.