Python is good at converting values to different data types. You can convert strings to integers, integers to strings, floats to integers, to name a few examples. There’s one conversion Python does not like: changing a float structured as a string to an integer.
In this tutorial, we discuss the ValueError: invalid literal for
int() with base 10 error and why it is raised. We walk through an example of this error to help you understand how you can fix it in your code.
The Problem: ValueError: invalid literal for int() with base 10
Let’s start by reading our error message:
ValueError: invalid literal for int() with base 10
Error messages have two parts. The first part tells us the type of error we are facing. A ValueError is raised when there is an issue with the value stored in a particular object.
Our error message tells us there is an invalid literal for an integer in base 10. This means the value we have passed through an
int() method cannot be converted.
int() method does not allow you to pass a float represented as a string. If you try to convert any string value not formatted as an integer, this error is raised.
This means you cannot convert a floating-point number in a string to an integer. In addition, you cannot convert letters to an integer (unless you are using letters with a special meaning, like “inf”).
An Example Scenario
Here, we build a program that calculates whether a coffee house has enough coffee in stock to serve their customers for a day. Our input field must accept decimal numbers because bags can be half full, a quarter full, and so on.
We convert the value a user inserts to an integer because we do not need to be precise down to the level of half-bags and quarter-bags.
Let’s start by asking the user to insert how many coffee bags are left using an input() statement:
coffee_bags = input("Enter how many coffee bags are left: ")
Next, we convert this value to an integer. We then use an “if” statement to check whether the coffee house has enough coffee. If the coffee house has over 10 bags, they have enough for the day. Otherwise, they do not.
Let’s write this into our program:
coffee_bags_as_int = int(coffee_bags) if coffee_bags_as_int > 10: print("You have enough coffee bags.") else: print("You do not have enough coffee bags.")
Let’s run our code and see what happens:
Enter how many coffee bags are left: 7.4 Traceback (most recent call last): File "main.py", line 3, in <module> coffee_bags_as_int = int(coffee_bags) ValueError: invalid literal for int() with base 10: '7.4'
When we try to write a decimal number into our program, an error is returned.
This error is caused because we try to convert “7.4: to an integer. The value “7.4” is formatted as a string. Python cannot convert a floating-point number in a string to an integer.
To overcome this issue, we need to convert the value a user inserts to a floating point number. Then, we can convert it to an integer.
We can do this by using the
int() statements. The
int() function returns an integer. The
float() function returns a floating-point representation of a float.
coffee_bags_as_int = int(float(coffee_bags))
Our code first converts the value of “coffee_bags” to a float. Next, it converts that value to an integer. Let’s try to run our code again:
Enter how many coffee bags are left: 7.4 You do not have enough coffee bags.
Our code works successfully. Now that we have converted “coffee_bags” to a float, our program can convert the value a user inserts into an integer.
The Python ValueError: invalid literal for
int() with base 10 error is raised when you try to convert a string value that is not formatted as an integer.
To solve this problem, you can use the
float() method to convert a floating-point number in a string to an integer. Then, you can use
int() to convert your number to an integer.
If this does not work, make sure that the value of a string does not contain any letters. Strings with letters cannot be converted to an integer unless those letters have a special meaning in Python.
Now you’re ready to solve this common Python error like an expert developer!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Read more