Python ValueError: invalid literal for int() with base 10: Solution

Posted in /  

Python ValueError: invalid literal for int() with base 10: Solution
vinaykhatri

Vinay Khatri
Last updated on April 25, 2024

    Python provides many inbuilt functions to convert a data type of one object to another, and one of those inbuilt functions is int() . int() function can convert a floating-point number and a valid string integer numerical value to a Python int object. But if we try to convert an invalid value to an integer number using the int() function, we receive the ValueError: invalid literal for int() with base 10 error.

    In this Python tutorial, we will discuss the ValueError: invalid literal for int() with base 10 error and why does it occur in a Program ? We will also discuss a common mistake that many python learners encounter this error. So let's get started with the error statement.

    Python Error ValueError: invalid literal for int() with base 10

    The error statement ValueError: invalid literal for int() with base 10 has two parts

    1. Exception Type ( ValueError )
    2. Error Message ( invalid literals for int() with base 10 )

    1. ValueError

    ValueError is one of the Python standard exceptions. It occurs in a Python program when an operation or function gets an argument of a correct data type but the wrong value.

    For example, the int() function can convert an integer string value to int type, but it can not convert other string values such as string floating-point and letters number to an integer (except for 'inf', 'Infinity', and 'nan').

    2. invalid literals for int() with base 10

    This error message tells us that the argument value passed to the int() function can not be converted into a base 10 integer value. The int() function can only convert floating-point numbers and string integer numeric values to integer base 10 numbers. The base 10 number represents the integer value range from 0 to 9. And if the value passed to the int() function is a string float or character; the Python interpreter will throw the ValueError with the Error message "invalid literals for int() with base 10".

    Example

    string_float = '20.12'
    
    # convert the string float into integer
    integer = int(string_float)
    
    print("The value of integer is: ", integer)

    Output

    Traceback (most recent call last):
        File "main.py", line 4, in <module>
        integer = int(string_float)
    ValueError: invalid literal for int() with base 10: '20.12'

    Break the code

    In this example, we are getting the error in line 4, where we are trying to convert them string_float into a int number using the int() function. The error was raised because the Python int() function can not convert a floating-point string value to an integer value.

    Solution

    If a string value is a floating-point number and we need to convert it into an integer. We convert that string value into float first using the float() function then convert it into the integer using int() function.

    string_float = '20.12'
    
    # convert the string float into float
    float_num = float(string_float)
    
    # convert the float to int
    integer = int(float_num)
    
    print("The value of integer is: ", integer)

    Output

    The value of integer is: 20

    Common Example Scenario

    In Python programming, we often use the int() function along with the input() function to convert the user-entered string numeric value to an integer number. Inside the input() message, we can ask the user to enter a numeric value, and what if the user enters a decimal numeric value instead of an integer?

    In that case, the int() object would not be able to convert the entered number into an integer and throw the ValueError.

    Example

    Let's write a Python program that asks the user to enter the number of plates they had in a buffet. The number of plates is supposed to be an integer number, but it is not necessary that the user has the full plate. It is also possible that they only had half in that case. The user may enter a floating-point number.

    plates_int = int(input("How many plates do you already have?: "))
    
    if plates_int>=5:
        print("You already have enough meal for today")
    else:
        print("You can go for 1 more plate")

    Output

    How many plates do you already have?: 4.5
    Traceback (most recent call last):
        File "main.py", line 1, in <module>
            plates_int = int(input("How many plates you have in buffet?: "))
    ValueError: invalid literal for int() with base 10: '4.5'

    Break the code

    In the above example, we entered ' 4.5' as the input value for plates_int variable, and when the int() function tries to convert the string value '4.5' into an integer, it threw the error.

    Solution

    To solve the above example, we need first to convert the input value to float using float() function, then we can convert it into an integer object using int() .

    # convert the user entered number into float
    plates_float = float(input("How many plates you have in buffet?: "))
    
    # convert the float number into int
    plates_int = int(plates_float)
    
    if plates_int>=5:
        print("You alredy have enough meal for today")
    else:
        print("You can go for 1 more plate")

    Output

    How many plates you have in buffet?: 4.5
    You can go for 1 more plate

    Conclusion

    In this article, we discussed why ValueError: invalid literal for int() with base 10 error occurs in Python, and how to debug it. This error is raised in a Python program when we pass an invalid string value to an int function, and the function is not able to convert the value to an integer number.

    If you are getting this error in your Python program, please share your code in the comment section. We will try to help you in debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments