ValueError in Python — What It Means and How to Fix It

What Python's ValueError exception means and how to resolve it.

Published October 26, 2026

ValueError is raised when a function receives an argument of the right type but an inappropriate value, such as int('abc').

What it means and why it happens

Typical causes: converting non-numeric strings, unpacking the wrong number of values or passing out-of-range values to functions.

Read the full message: it names the file and line where the problem was detected, which is the best place to start.

What you see

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

Practical examples

Code that triggers it

n = int('abc')

The fix

text = 'abc'
try:
    n = int(text)
except ValueError:
    n = 0

Validate first

if text.strip().lstrip('-').isdigit():
    n = int(text)

Common mistakes

  • Not validating user input.
  • Catching ValueError too broadly.
  • Confusing with TypeError (wrong type, not wrong value).

Key takeaways

  • ValueError: ValueError is raised when a function receives an argument of the right type but an inappropriate value, such as int('abc').
  • Read the message and the line number first.
  • Fix the cause, don't just silence the error.

FAQ

ValueError vs TypeError?

TypeError is the wrong type; ValueError is the right type with a bad value.

How do I find which line caused ValueError?

The error message and stack trace show the file and line; start there and work backwards.

More Python articles