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

Python provides various functions that are used to convert one data type into another, such as int(), float(), str(), etc. However, these functions throw an error when an invalid value is passed as an argument. For instance, when a string or float value is passed as an argument to the int() function, the “invalid literal for int() with base 10” error arises in Python. To fix this error, different solutions are provided by Python.

This write-up will deliver different reasons and solutions regarding “ValueError: invalid literal for int() with base 10”. The following reason and solutions will be covered in this blog post:

Reason 1: Passing Float String Value

The prominent reason which causes this error in Python is when the user tries to pass a float string value to an “int()” function.

The above output shows the “ValueError” because a non-integer value “44.5” is passed inside the “int()” function.

Solution: Use float() Function

To resolve this error, the “float()” function must be utilized that will convert the floating point string into float. Below is an example of this solution:

Code:

num = input("Enter Number: ")
int_num = int(float(num))

if int_num > 50:
    print("Number is Greater than 50")
else:
    print("Number is Smaller than 50")

In the above code, the “input()” function accepts the value from the user. Next, the user-input value is converted into float and integer using the “float()” and “int()” functions. Afterward, the “if-else” statement is utilized to check the condition and execute the statements accordingly.

Output:

The above output verifies that converting the input float type string into float data type resolves the stated error.

Reason 2: Passing Alphabetic String Value

Another main reason which causes this error in Python is when a user tries to input an alphabetic string to an “int()” function. An example of it is shown in the below snippet:

The above output shows the “ValueError” because a character string value “Lily” is passed inside the “int()” function.

Solution 1: Using the isdigit() Function

To solve this error, the “isdigit()” function can be utilized in the Python program. The function is used to check whether the input number is an integer.

Code:

num = input("Enter Number: ")
if num.isdigit():
    int_num = int(num)
   
    if int_num > 50:
        print("Number is Greater than 50")
    else:
        print("Number is Smaller than 50")
else:
    print('Please Enter Valid Numeric Number')

In the above code, the “isdigit()” function is used to check whether the input value taken from the user is a digit or not. if the input number is a digit, then the program written inside the “if” block will be executed and displayed on the screen. If the input number is a string or float, then the else block is executed.

Output:

The above output verified that the input number is not a valid numeric value.

Solution 2: Using try-except Block

To resolve this error, use the “try-except” block in the program. The “try” block tries to execute the program when there is no error, but when an error arises in the program, the “except” block handles the error by showing an exception message. Here is an example:

Code:

num = input("Enter Number: ")

try:
    int_num = int(float(num))   
    if int_num > 50:
        print("Number is Greater than 50")
    else:
        print("Number is Smaller than 50")
except:
    print('Please Enter Valid Numeric Number')

In the above code, the “try” block executes the program only if the input value is numeric. When a user enters a non-numeric value, the “ValueError” arises, and to handle that error, the “except” block executes its statement.

Output:

The “except” block executed its statement when a non-numeric value was passed inside the int() function.

Conclusion

The “ValueError: invalid literal for int() with base 10” occurs when a user tries to pass a non-numeric value, string value, as an argument to the int() function. To resolve this error “float()” function, “isdigit()” function, and “try-except” block are used in Python. The float() function converts the numeric float string into float and then converts it into int using the int() function. Similarly, the “isdigit()” function is used to check whether the input number is a digit and perform operations on it accordingly. This Python article explained how to fix the “invalid literal for int() with base 10” error in Python.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together. Click here to join us! ×