Ever got this python error? invalid literal for int with base 10 Or some sort of this same message? Keep reading to know the solution for this Python error and to master the data types conversions.
Python Error: Invalid Literal for Int with Base 10
In this post, I will explain the solution for the “invalid literal for int” errors on Python. We all know that with python is very handy when converting between different data types easily. From strings to integers, from integers to floats, passing a string to float…
But, what if you want to convert from a string representing a float to an integer?
Well, then python will raise an error saying it can’t! See the example below:
# String with some numbers (float) strval = "152.569" intval = int(strval) # Output: Traceback (most recent call last): File "main.py", line 3, in <module> intval = int(strval) ValueError: invalid literal for int() with base 10: '152.569'
What python is telling is simply that the number in the string is not a in a base 10. Simply put, it has decimal numbers and the int() function cannot deal with decimal values from a string!
This error will also be raised if you try to convert a string with letters to an integer. Unless we use specific representation like the ‘Inf’ value.
Example of use
Let us build an example to see how we can fix this issue. Let us say that we have a python script that asks for the age of the user and then tells you which year the user was born:
# Let's ask first the user for his age: age = input("Enter how old are you? ") agenumber = int(age) yearborn = 2021 - agenumber print(yearborn)
Obviously, we are only expecting integers in our script (it doesn’t make sense for someone to not put full years, right?). So what happens if the user puts a number that is not an integer?
Enter how old are you? 7.4 Traceback (most recent call last): File "main.py", line 3, in <module> agenumber = int(age) ValueError: invalid literal for int() with base 10: '7.4'
Oups! As we said, the value of 7.4 is not a base 10 value which means can’t be converted to Integer!
How to solve invalid literal for int() problem
As I explained above, this error is raised if the value we pass to int() is not a base 10 number. How can we overcome this then? A solution is to pass the value we ask the user to a float value and then to an integer value!
So let’s fix our code above:
# Let's ask first the user for his age: age = input("Enter how old are you? ") agenumber = int(float(age)) yearborn = 2021 - agenumber print(yearborn)
By using the float() function we are asking to convert the variable age to float and then we convert it to integer using the int () function.
Enter how old are you? 20.5 2001
Our code now is more robust and works as expected!
As we have seen, the error invalid literal for int with base 10 is raised when we are trying to convert a string with decimals to an integer value.
Overcoming this problem is quite easy, as seen in our example. We just need to convert first from string to float and then we convert to integer. The functions we use are float() and int() respectively.
We can still have issues if the string we are using contains any letters. We have to make sure that the user/context is only valid with numbers.
Hope we helped with your problems in Python! You can post below if you have any question regarding this!
Check our Coding category to see other topics!