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

## How to Copy a File in Python?

Table of Contents Hide Modules to copy a file in Python shutil module top copy a file in Pythoncopy() copy2() copyfile() copyfileobj()os module to copy a file in Pythonpopen() system() subprocess…

## How to Create a Pie Chart in Seaborn

We do not have any built-in function to create Pie chart in seaborn, but with the help of Matplotlib, we can create a pie chart and leverage seaborn for color…

## SyntaxError: unexpected character after line continuation character

Table of Contents Hide SyntaxError: unexpected character after line continuation character.Fixing unexpected character after line continuation characterUsing backslash as division operator in PythonAdding any character right after the escape characterAdding any…

## TypeError: list indices must be integers or slices, not tuple

If you are accessing the list elements in Python, you need to access it using its index position. If you specify a tuple or a list as an index, Python…

## numpy.median() Function

Table of Contents Hide What is a Median?SyntaxParametersReturn ValueExample 1: Calculate the median of the array using numpy.median() methodExample 2: Calculate the median of the array as column-major orderExample 3:…

## ‘python’ is not recognized as an internal or external command, operable program or batch file.

The ‘python’ is not recognized as an internal or external command, operable program or batch file. error is encountered in the command prompt of Windows after you install Python on the windows…