Python Error

ValueError

ValueError is raised when a function receives an argument of the right type but an inappropriate value. For example, int('abc') raises ValueError because 'abc' is a string (correct type) but not a valid integer representation.

Common causes

  • Converting invalid strings to numbers: int("abc")
  • Unpacking wrong number of values
  • Passing out-of-range values to math functions
  • Invalid arguments to list.remove() for missing elements
  • Incorrect date/time format strings

How to fix it

  • Validate input before conversion
  • Use try/except ValueError to handle conversion failures
  • Check value ranges before passing to functions
  • Use "if item in list" before list.remove(item)
  • Parse dates with error handling using strptime

Example

ValueError example
# Error example
number = int("abc")
# ValueError: invalid literal for int() with base 10: 'abc'

a, b = [1, 2, 3]
# ValueError: too many values to unpack (expected 2)

# Fix with try/except
def safe_int(value, default=0):
    try:
        return int(value)
    except ValueError:
        return default

safe_int("abc")  # 0

# Fix unpacking
a, b, *rest = [1, 2, 3]  # a=1, b=2, rest=[3]

# Validate before removing
items = [1, 2, 3]
if 4 in items:
    items.remove(4)

Track ValueError with Checkend

Checkend automatically captures ValueError errors in your Python application with full context:

  • Complete traceback with syntax highlighting
  • Request context (URL, params, headers)
  • Automatic grouping of similar errors
  • Instant notifications when errors occur

Stop debugging in production

Get full error context and fix issues faster with self-hosted error tracking.