ValueError: Invalid Literal for int()

ValueError: invalid literal for int() with base 10: 'abc'

Quick Answer

You are converting a string to integer but the string is not a valid number. Clean the input or validate before converting.

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'))  # 19

Step-by-Step Fix

  1. 1

    Validate input

    Use str.isdigit() or try/except ValueError.

  2. 2

    Strip whitespace

    Clean with .strip() before converting.

  3. 3

    Use float() for decimals

    Use float() or int(float(s)) for decimal strings.

Bugsly 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