Ad Space — Top Banner

ValueError

Python Programming Language

Severity: Moderate

What Does This Error Mean?

A ValueError means you gave a function a value of the right type, but the value itself does not make sense for what the function does. For example, trying to convert the word 'hello' into a number, or finding the square root of a negative number. The type is fine, but the value inside it is not acceptable.

Affected Models

  • Python 2.x
  • Python 3.x
  • All Python versions

Common Causes

  • Calling int() or float() on a string that does not contain a valid number (like int('hello'))
  • Trying to unpack a list into variables when the number of items does not match (like: a, b = [1, 2, 3])
  • Passing a value outside the accepted range to a math function (like math.sqrt(-1))
  • Searching for an item in a list with .remove() when that item does not exist
  • Converting a date string in the wrong format — the format must match exactly

How to Fix It

  1. Read the error message. It usually names the function that raised the error and sometimes describes why the value was rejected.

    For example: 'invalid literal for int() with base 10: hello' means you tried to convert the word 'hello' into an integer.

  2. If you are converting user input to a number, wrap it in a try/except block to handle the case where the user types something that is not a number.

    Example: try: x = int(input('Enter a number: ')) except ValueError: print('That is not a valid number.')

  3. If you are unpacking a list or tuple into variables, make sure the number of variables matches the number of items exactly.

    Example: a, b, c = [1, 2, 3] works. a, b = [1, 2, 3] raises ValueError: too many values to unpack.

  4. If you are parsing a date, make sure the format string matches the actual date string exactly. The format codes are things like %Y for a 4-digit year, %m for month, %d for day.

    Example: datetime.strptime('28/03/2026', '%d/%m/%Y') — the format must match the string character by character.

  5. Validate your data before passing it to functions. Check that a string is numeric with .isdigit() before calling int() on it, or that a list has the expected number of items before unpacking.

    Preventing a ValueError is always better than catching it after the fact.

When to Call a Professional

ValueErrors are always something you can fix yourself. The error message usually names the function that rejected the value. Add input validation — check values before passing them to a function that might reject them.

Frequently Asked Questions

What is the difference between a ValueError and a TypeError?

A TypeError is about the wrong type — like passing a string where a number is required. A ValueError is about the right type but a bad value — like passing a string to int(), but the string contains letters instead of digits. The type check passed, but the value check failed.

How do I safely convert user input to a number?

Always use try/except when converting input. Example: try int(user_input) and catch the ValueError to show a friendly error message. Never assume the user typed a valid number — they almost always type something unexpected eventually.

Why does int('3.14') raise a ValueError?

Because '3.14' is not a whole number as a string — it contains a decimal point. int() only accepts strings that look like whole numbers (like '3' or '42'). If you need to convert '3.14', first use float('3.14') to get the decimal number, then use int() on that if you want to drop the decimal part.