TheAlgorithms/Python · error · ValueError

Must be 10 characters long

Error message

Must be 10 characters long

What it means

Raised by zeller(date_input) in maths/zellers_congruence.py when the input string is not within 1..10 characters, i.e. not the expected 10-character MM-DD-YYYY or MM/DD/YYYY shape. It is the first validation in the function, so it fires before month/day/separator checks. The message says 'Must be 10 characters long' although the code actually accepts any length from 1 to 10 due to the 0 < len < 11 condition.

Source

Thrown at maths/zellers_congruence.py:89

        ...
    ValueError: Must be 10 characters long"""

    # Days of the week for response
    days = {
        "0": "Sunday",
        "1": "Monday",
        "2": "Tuesday",
        "3": "Wednesday",
        "4": "Thursday",
        "5": "Friday",
        "6": "Saturday",
    }

    convert_datetime_days = {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 0}

    # Validate
    if not 0 < len(date_input) < 11:
        raise ValueError("Must be 10 characters long")

    # Get month
    m: int = int(date_input[0] + date_input[1])
    # Validate
    if not 0 < m < 13:
        raise ValueError("Month must be between 1 - 12")

    sep_1: str = date_input[2]
    # Validate
    if sep_1 not in ["-", "/"]:
        raise ValueError("Date separator must be '-' or '/'")

    # Get day
    d: int = int(date_input[3] + date_input[4])
    # Validate
    if not 0 < d < 32:
        raise ValueError("Date must be between 1 - 31")

View on GitHub (pinned to f5988cc097)

Solutions

  1. Normalize your date to the 'MM-DD-YYYY' string format (e.g. dt.strftime('%m-%d-%Y')) before calling zeller.
  2. If input can vary, pre-validate with a regex or strptime('mm-dd-yyyy') and reject early.
  3. Trim accidental whitespace/newlines from read lines before passing them in.

Example fix

# before
zeller('2010-01-31')  # wrong order + wrong shape

# after
import datetime
zeller(datetime.date(2010, 1, 31).strftime('%m-%d-%Y'))  # '01-31-2010'
Defensive patterns

Strategy: validation

Validate before calling

import re
ZELLER_RE = re.compile(r'^\d{2}[-/]\d{2}[-/]\d{4}$')

def is_zeller_date(s: str) -> bool:
    return isinstance(s, str) and bool(ZELLER_RE.match(s))

Type guard

def is_zeller_date(s: str) -> TypeGuard[str]:
    return isinstance(s, str) and len(s) == 10 and bool(ZELLER_RE.match(s))

Try / catch

try:
    day = zeller(date_str)
except ValueError as e:
    # covers length, month, day, separator, year errors
    return f'unrecognized date {date_str!r}: {e}'

Prevention

When it happens

Trigger: Calling zeller('') (empty string) or zeller('01-31-19082939') (over-long year); passing ISO dates like '2010-01-31' or datetime objects/None, which hit this or a later type error before any parsing succeeds.

Common situations: Feeding ISO-format dates (YYYY-MM-DD) into a function expecting MM-DD-YYYY; passing user input without normalizing; passing a date object instead of its string representation.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/244bed423d4ffa8a. Report an issue: GitHub.