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

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 ## ModuleNotFoundError: No module named ‘django’

Table of Contents Hide What is ModuleNotFoundError: No module named ‘django’?How to fix ModuleNotFoundError: No module named ‘django’?Solution 1 – Installing and using the Django module in a proper waySolution… ## ModuleNotFoundError: No module named ‘flask_sqlalchemy’

Table of Contents Hide What is ModuleNotFoundError: No module named ‘flask_sqlalchemy’?How to fix ModuleNotFoundError: No module named ‘flask_sqlalchemy’?Solution 1 – Installing and using the Flask-SQLAlchemy module in a proper waySolution… ## Python Print to File

We always use print statements in Python to display the output in the console or command line terminal. However, sometimes we want to change this behavior to print to a… ## Python Convert Bytes to String

In this tutorial, we will take a look at how to convert bytes to string in Python.  We can convert bytes to string using the below methods  Using decode() method… ## Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.

Table of Contents Hide How to solve Python was not found; run without arguments to install from the Microsoft StoreScenario 1: Python is not installedScenario 2: Environment variables are not… ## Python List index()

Table of Contents Hide Syntax of List index() index() ParametersReturn Value from List index()Example 1: Find the index of the elementExample 2: Index of the Element not Present in the List…