The number of plates is supposed to be an integer number, but it is not necessary that the user had the full plate. Let’s write a Python program that asks the user to enter the number of plates they had in a buffet. In that case, the int() object would not be able to convert the entered number into an integer and throw the ValueError. 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 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. Output The value of integer is: 20 Common Example Scenario We convert that string value into float first using float() function then convert it into the integer using int() function. If a string value is a floating-point number and we need to convert it into an integer. The error was raised because the Python int() funciton can not convert a floating-point string value to an integer value. 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 int() function. ValueError: invalid literal for int() with base 10: '20.12' Output Traceback (most recent call last):
Print("The value of integer is: ", integer) The base 10 number represent the integer value range from 0 to 9.Īnd if the value passed to the int() function is a string float or character, the Python interpreter will throw the ValueError with Error message “invalid literals for int() with base 10”. The int() function can only convert floating points numbers and string integer numeric values to integer base 10 numbers. This error message tells us that the argument value passed to the int() function can not be converted into a base 10 integer value. It occurred in a Python program when an operation or function get an argument of a correct data type but the wrong value.įor 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’). ValueError is one of the Python standard exceptions.