# ValueError: invalid literal for int() with base 10

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
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
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
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
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.

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

##### You May Also Like ## How to Check the NumPy Version

Table of Contents Hide Check the NumPy version using __version__ attributeGet the Detailed Version of NumPy Get the NumPy version using pip3Verify the NumPy version on Anaconda Distribution.Print the NumPy version… ## TypeError: can only concatenate str (not “int”) to str

Table of Contents Hide What is TypeError: can only concatenate str (not “int”) to strExample ScenarioHow to fix TypeError: can only concatenate str (not “int”) to strSolutionConclusion In Python, we… ## Python syntaxerror: eol while scanning string literal

Table of Contents Hide What does EOL while scanning string literal error mean in Python?Missing quotation marksString spanning multiple lines using the wrong syntaxMismatch of quotesUsing backslash before the ending… ## Python String isupper()

Table of Contents Hide SyntaxParameterReturn ValueExample 1: Demonstrating the working of isupper() method Example 2: Practical use case of isupper() in a program Python String isupper() method is a built-in function… ## Python Dictionary get()

Table of Contents Hide get() Syntaxget() Parametersget() Return ValueExample 1 – How to use the get() method in DictionaryExample 2 – Difference between get() method and dict[key] to access elements Python… ## Calculate Euclidean Distance in Python

Table of Contents Hide How to Calculate Euclidean Distance in Python?Method 1: Using linalg.norm() Method in NumPyMethod 2: Using dot() and sqrt() methodsMethod 3: Using square() and sum() methodsMethod 4:…