. Advertisement .
..3..
. Advertisement .
..4..
“Invalid literal for int() with base 10” is a value error often seen in many Python programs. Fortunately, its antidote/solution is simpler than you think! Scroll to read our analysis and instructions.
An Example of The Error
In this example, we try to convert our input values into integers, meaning our input height is supposed to always be integer. Yet, users can type in the height even via decimal value. Hence, once we attempt to change it into integers, Python will throw us the error message.
number= input("What is your height:")
centimeters=int(number)
print ("The height of the person is:" + str(centimeters))
Output/Error:
What is your height:155.5
Traceback (most recent call last):
File "c:\Projects\Tryouts\listindexerror.py", line 2, in <module>
centimeters=int(number)
ValueError: invalid literal for int() with base 10: '155.5'
During the code execution, we assume Python will truncate decimal values automatically to only keep the integer parts. Our function Int() uses decimal systems as its base conversion, which means “base=10” serves as the transformation’s default value.
Hence, only the conversions of the Int’s string representation are possible – not float, decimal, or chars.
How to Solve The Error “Invalid Literal For Int() With Base 10”?
Method 1. Convert Inputs Into Floats
You can convert input numbers into floats via the method Float(). Next, parse your decimal digits before converting them once more into integers. The example below will show you how to do this.
number= input("What is your height:")
centimeters=int(float(number))
print ("The height of the person is:" + str(centimeters))
Output:
What is your height:155.5
The weight of the person is:155.5
Method 2. Check The Numeric Input Values
In certain scenarios, your entered input might lie in a string. Hence, converting string values will send an Error message even after we have used Solution 1 illustrated above.
Another alternative approach is to confirm whether your entered inputs are numeric digits or not. Use the method isdigit(), which will return “True” if it’s numeric and “False” if non-numeric.
number= input("What is your height:")
if number.isdigit():
centimeters=int(float(number))
print ("The height of the person is:" + str(centimeters))
else:
print("Error - Please enter a proper height")
Output
What is your height:test
Error - Please enter a proper height
Method 3. Use Try Except
Another great way to tackle these errors is the command “Try Except.”
number= input("What is your height:")
try:
centimeters=int(float(number))
print ("The height of the person is:" + str(centimeters))
except:
print("Error - Please enter a proper height")
Output
What is your height:test
Error - Please enter a proper height
Conclusion
Fixing the error “Invalid literal for int() with base 10” will no longer be challenging for you after this insightful article. For other value errors in Python (such as “Mime type rendering requires nbformat“), keep browsing our website!
Leave a comment