To solve the error, you can use the float() method to convert entered decimal input and then use the int() method to convert your number to an integer. ValueError: invalid literal for int() with base 10 occurs when you convert the string or decimal or characters values not formatted as an integer. Solution 3: The other common way to handle this kind of errors is using try except number= input("What is your weight:")Įrror - Please enter a proper weight Conclusion Print("Error - Please enter a proper weight") Python has isdigit() method, which returns true in case of a numeric value and false if it’s non-numeric. So better way to solve this is to ensure the entered input is a numeric digit or not.
Solution 2: There can be other possible issues where the entered input value itself might be in the string, so converting the string value will throw a Value Error even if we use the above method. Here's the traceback Traceback (most recent call last): File 'C:\Users\kasutaja\Desktop\EksponentfunktsioonTEST - koopia.py', line 13, inSolution 1: We can first convert the input number into a float using float() method, parse the decimal digits, and convert them again into an integer, as shown below. Therefore it can only convert the string representation of int, not the decimal or float or chars.
The int() function uses the decimal number system as base conversion, meaning base=10 is the default value for transformation. One can think that while executing the above code, Python will automatically truncate the decimal value and retain only the integer part. ValueError: invalid literal for int() with base 10: '55.5' Print ("The weight of the person is:" + str(kilos))įile "c:\Projects\Tryouts\listindexerror.py", line 2, in