Why This Happens
The int() function can only parse strings representing whole numbers. Strings with letters, spaces, decimals, or special characters cause this error.
The Problem
age = int(input('Enter age: ')) # Fails if user types 'abc'
price = int('19.99')The Fix
try:
age = int(input('Enter age: '))
except ValueError:
print('Please enter a valid number')
price = int(float('19.99')) # 19Step-by-Step Fix
- 1
Validate input
Use str.isdigit() or try/except ValueError.
- 2
Strip whitespace
Clean with .strip() before converting.
- 3
Use float() for decimals
Use float() or int(float(s)) for decimal strings.
Got the actual stack trace?
Paste it into our free AI explainer to get the cause and the fix for your specific case — no signup, nothing to install.
Explain my errorBugsly catches this automatically
Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.
Try Bugsly free