Python ValueError: invalid literal for int() with base 10
Why does this occur?
While using the
int() function, there are some strict rules which we must follow.
This error is triggered in one of two cases:
- When passing a string containing anything that isn't a number to
int(). Unfortunately, integer-type objects can't have any letters or special characters.
- When passing
int()a string-type object which looks like a float-type (e.g., the string
'56.3'). Although technically, this is an extension of the first error case, Python recognizes the special character
.inside the string
To avoid the error, we shouldn't pass
int() any letters or special characters.
We'll look at a specific example for each of these causes, along with how to implement a solution.
Cause 1: Non-numerical Arguments
As mentioned previously, one of the most common causes of the
ValueError we've been looking at is passing
int() an argument that contains letters or special characters.
By definition, an integer is a whole number, so an integer-type object should only have numbers (
- are also acceptable). As a result, Python will throw an error when attempting to convert letters into an integer.
This error can frequently occur when converting user-input to an integer-type using the
int() function. This problem happens because Python stores the input as a string whenever we use
As a result, if you'd like to do some calculations using user input, the input needs to be converted into an integer or float.
Let's consider a basic example and create a short program that will find the sum of two values input by the user:
As you can see, we received "53" instead of the correct sum, "8". We received this output because the input must be converted to integer-type to calculate the sum correctly. We have instead added two strings together through concatenation.
So, we need to convert the values to integers before summing them, like so:
We're now getting the correct answer.
Despite working as expected, problems can occur if users start inputting values that aren't integers.
In the example below, we have entered "Hello" as the second input:
We now have an error because the value
'Hello', which isn't a number. We can fix this by using a simple try/except block, like in the following solution.
In this scenario, there isn't much that can be done about users testing the limits of our program. One potential solution is adding an exception handler to catch the error and alert the user that their entry was invalid.
Using an exception handler here, the user will receive a warning message whenever
int() throws a
ValueError. This solution is a common way to handle user input that will be cast to integers.
Cause 2: Float-like Strings
int() a string-type object which looks like a float (e.g.
'56.9'), will also trigger the
In this scenario, Python detects the
., which is a special character, causing the error. For example, here's what happens when we pass
int() as an argument:
int() works with floats, so the simplest way to fix the error, in this case, is to convert our string to a floating-point number first. After converting the string-type to float-type, we can successfully pass our object to the
The main problem with this approach is that using
int() on a float will cut off everything after the decimal without round to the nearest integer.
56.9 is much closer to
int() has performed a simple truncation to eliminate the decimal.
For situations where you'd prefer to round floats to their closest integer, you can add an intermediate step using the
round() function, like so:
Specifying zero as our second
round() argument communicates to Python that we'd like to round
val to zero decimal places (forming an integer). By altering the second argument, we can adjust how many decimal numbers Python should use when rounding.
As we've discussed, passing an argument that contains letters or special characters to the
int() function causes this error to occur. Integers are whole numbers, so any string-type objects passed to
int()should only contain numbers,
In cases where we'd like to convert a string-type object that looks like float (e.g.
'56.3') into an integer, we will also trigger the error. The error happens due to the presence of the
.character. We can easily avoid the error by converting the string to a floating-point object first, as follows:
In cases where we'd like a number to remain a decimal, we could drop the
int() function altogether and use
float() to make the object a float-type. Ultimately, if you're experiencing this error, it's a good idea to think about what's going into the
int() function and why that could be causing problems.
The error is straightforward to resolve once you get to the bottom of what's causing the issue.