Solve “ValueError: invalid literal for int() with base 10” in Python Neeraj Mishra The Crazy Programmer
Hello everyone, So today we’re going to see the possible reasons and solutions for the error “ValueError: invalid literal for int() with base 10” in Python.
The main reason for this error is that in our program, we’re passing a non-integer value to a place where the interpreter expects an integer value.
To understand the above lines, let’s take an example.
age = int(input("Enter your age: "))
When you run the above line, the interpreter will ask you to enter your age, when you will enter the age in numbers this program will be executed successfully, but if you’ll enter some string like
“Twenty three” then it will throw the error “ValueError: Invalid literal for int() with base 10”.
Because input() function returns a string and outside input() function we’re trying to convert the string into an integer using int(). So the string should be convertible to int, just like a number without any spaces. But when we pass the string that contains characters or spaces it will throw an error.
So now you’ve got the basic idea behind this error, let’s see the common mistakes we do that led us to this error –
Scenario 1:
amount = int(input("enter the amount:")) print("amount = ", amount)
Output 1:
enter the amount:3400
amount = 3400
Output 2:
enter the amount:3400.5
Traceback (most recent call last):
File “main.py”, line 1, in <module>
amount = int(input(“enter the amount:”))
ValueError: invalid literal for int() with base 10: ‘3400.5’
You’ve seen that in the first output the program runs successfully, But when we pass a decimal value it crashes with the same error. Normally, we can convert a float value to int using int(), So what went wrong now?
Actually, as we know the input() function returns a string, not afloat value, So that’s why this time int() was unable to convert this to an integer and thrown this error.
To solve it, we can write our program in this way:
amount = int(float(input("enter the amount:"))) print("amount = ", amount)
In this way, any value entered by the user will be converted to float first then to an integer. So it won’t throw any error if we pass a float value.
Scenario 2:
age = int(input("enter the age:")) print("age = ", age)
Output 1:
enter the age:23
age = 23
Output 2:
enter the age:twenty three
Traceback (most recent call last):
File “main.py”, line 1, in <module>
age = int(input(“enter the age:”))
ValueError: invalid literal for int() with base 10: ‘twenty three’
The controls at the run time always will be in the hand of the user, So we can’t control it. But what we can do is to handle this using try-except blocks and throw a proper message so user can enter a valid integer.
try: age = int(input("enter the age:")) print("age = ", age) except ValueError: print("please enter age in digits")
Output:
enter the age: twenty three
please enter age in digits
So in this way, our program doesn’t crash and user will also understand the message.
Conclusion
In the end, we can say that this problem mostly occurs when we’re passing a string instead of integer where an integer is required, like in two above examples in int() function we need to pass the value that can be converted to an integer.
This problem can occur while reading the integer data from files (if spaces are there), the user passes an empty string in int().
So that was all for this article, if you’re still facing any issue, you can let us know in the comment box.
The post Solve “ValueError: invalid literal for int() with base 10” in Python appeared first on The Crazy Programmer.
from The Crazy Programmer https://ift.tt/2Y5M7Fd
Comments
Post a Comment