Common causes
- The string contains a decimal point: int('3.14') — use float() first or int(float('3.14'))
- User input contains letters or extra whitespace: int('12px') or int('')
- Reading from a file or API and the value isn't what you expected
- An empty string or None was passed to int()
How to fix it
- Strip whitespace before converting: int(value.strip())
- For decimal strings, convert via float: int(float('3.14'))
- Validate the input first: if value.isdigit(): int(value)
- Use a try/except block to handle invalid values gracefully: try: int(value) except ValueError: ...
Example
Traceback (most recent call last):
File "app.py", line 2, in <module>
age = int(input_value)
ValueError: invalid literal for int() with base 10: '25 years'Calling int() on a string that contains text characters after the number
Have a different error?
Paste any error message into the Error Translator to get an instant explanation.
