Python

ValueError: invalid literal for int()

ValueError

A function received a value of the right type but with invalid content (e.g., int('abc')).

Common Causes

Fixes

  1. 1. Validate before converting
    s = input().strip()
    if s.isdigit():
        n = int(s)
  2. 2. Use try/except
    try:
        n = int(s)
    except ValueError:
        n = default
  3. 3. Handle empty input
    n = int(s) if s else 0

Still not fixed?

Related Errors

DuskTools That Might Help

All Errors