Python Forum
Thread Rating:
  • 3 Vote(s) - 2.33 Average
  • 1
  • 2
  • 3
  • 4
  • 5
int() ValueError
#1
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?
Reply
#2
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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020