ValueError: invalid literal for int() with base 10

ItsMyCode |

ValueError occurs when we pass an invalid argument type. The error is raised when we call int()function with string argument which Python cannot parse and throws ValueError: invalid literal for int() with base 10: ”

*Fix * ValueError: invalid literal for int() with base 10

Let’s look at some examples and the solution to fix the ValueError in Python.

Example – Converting float to integer

If you look at the below example, we are trying to convert the input value into an integer that means we expect that the input field weight is always an integer value.

However, the user can enter weight even in decimal value, and when we try to convert it into an integer, Python throws invalid literal for int() with base 10 error.

number= input("What is your weight:")
kilos=int(number)
print ("The weight of the person is:" + str(kilos))

# Output
What is your weight:55.5
Traceback (most recent call last):
  File "c:\Projects\Tryouts\listindexerror.py", line 2, in <module>
    kilos=int(number)
ValueError: invalid literal for int() with base 10: '55.5'

One can think that while executing the above code, Python will automatically truncate the decimal value and retain only the integer part. The int()function uses the decimal number system as base conversion, meaning base=10 is the default value for transformation. Therefore it can only convert the string representation of int, not the decimal or float or chars.

Solution 1: We can first convert the input number into a float using float() method, parse the decimal digits, and convert them again into an integer, as shown below.

number= input("What is your weight:")
kilos=int(float(number))
print ("The weight of the person is:" + str(kilos))

# Output
What is your weight:55.5
The weight of the person is:55

Solution 2: There can be other possible issues where the entered input value itself might be in the string, so converting the string value will throw a Value Error even if we use the above method.

So better way to solve this is to ensure the entered input is a numeric digit or not. Python has isdigit() method, which returns true in case of a numeric value and false if it’s non-numeric.

number= input("What is your weight:")
if number.isdigit():
    kilos=int(float(number))
    print ("The weight of the person is:" + str(kilos))
else:
    print("Error - Please enter a proper weight")

# Output
What is your weight:test
Error - Please enter a proper weight

Solution 3: The other common way to handle this kind of errors is using try except

number= input("What is your weight:")
try:
    kilos=int(float(number))
    print ("The weight of the person is:" + str(kilos))
except:
    print("Error - Please enter a proper weight")

# Output
What is your weight:test
Error - Please enter a proper weight

Conclusion

ValueError: invalid literal for int() with base 10 occurs when you convert the string or decimal or characters values not formatted as an integer.

To solve the error, you can use the float() method to convert entered decimal input and then use the int() method to convert your number to an integer. Alternatively, you can use the*isdigit()* method to check if the entered number is a digit or not, and the final way is to use try/except to handle the unknown errors.

25