Error guide · ValueError
ValueError: invalid literal for int() with base 10
The type of value is right, but the value itself cannot be used.
Example
text = "12a" number = int(text) print(number + 1)
What Python says
ValueError: invalid literal for int() with base 10: '12a'
As printed by Python 3.12. Other versions may word it slightly differently.
Why it happens
int() accepts strings, but only strings that look like a whole number. "12a", "3.5" and "" (an empty string, often from pressing Enter without typing) cannot become an int.
How to fix it
text = "12" number = int(text) print(number + 1)
Output: 13
Tips
- Use
float()for numbers with a decimal point, thenint()if you need a whole number. - Check the program input: an extra space line or a letter causes this error.
- Use
try/except ValueErrorto handle bad input politely.