TheAlgorithms/Python · error · ValueError

Expecting {number_of_bands} colors, provided {len(colors)} c

Error message

Expecting {number_of_bands} colors, provided {len(colors)} colors

What it means

Raised by check_validity() in electronics/resistor_color_code.py when len(colors) differs from number_of_bands (after number_of_bands has already passed the 3-6 range check). A resistor's color list must contain exactly one color per band: 3 bands means 3 colors, 4 means 4, etc.

Source

Thrown at electronics/resistor_color_code.py:289

      ...
    ValueError: Expecting 4 colors, provided 3 colors

    >>> check_validity(3, ["Cyan","Red","Yellow"])
    Traceback (most recent call last):
      ...
    ValueError: Cyan is not a valid color

    """
    if number_of_bands >= 3 and number_of_bands <= 6:
        if number_of_bands == len(colors):
            for color in colors:
                if color not in valid_colors:
                    msg = f"{color} is not a valid color"
                    raise ValueError(msg)
            return True
        else:
            msg = f"Expecting {number_of_bands} colors, provided {len(colors)} colors"
            raise ValueError(msg)
    else:
        msg = "Invalid number of bands. Resistor bands must be 3 to 6"
        raise ValueError(msg)


def calculate_resistance(number_of_bands: int, color_code_list: list) -> dict:
    """
    Function calculates the total resistance of the resistor using the color codes.
    Function takes number_of_bands, color_code_list as input and returns
    resistance

    >>> calculate_resistance(3, ["Black","Blue","Orange"])
    {'resistance': '6000Ω ±20% '}

    >>> calculate_resistance(4, ["Orange","Green","Blue","Gold"])
    {'resistance': '35000000Ω ±5% '}

    >>> calculate_resistance(5, ["Violet","Brown","Grey","Silver","Green"])

View on GitHub (pinned to f5988cc097)

Solutions

  1. Count the resistor's physical bands first and pass exactly that many colors, in band order (significant digits, multiplier, tolerance, temp coefficient).
  2. Validate len(colors) == number_of_bands before calling check_validity or calculate_resistance.
  3. For a 4-band resistor remember the layout is digit-digit-multiplier-tolerance (4 colors), not 3.

Example fix

# before
calculate_resistance(4, ["Violet", "Brown", "Grey", "Silver", "Green"])  # 5 colors

# after
calculate_resistance(5, ["Violet", "Brown", "Grey", "Silver", "Green"])  # 5 bands, 5 colors
Defensive patterns

Strategy: validation

Validate before calling

if len(color_code_list) != number_of_bands:
    raise UserInputError(
        f"Need exactly {number_of_bands} colors, got {len(color_code_list)}"
    )

Try / catch

try:
    result = calculate_resistance(number_of_bands, color_code_list)
except ValueError as exc:
    logger.warning("Resistor input rejected: %s", exc)
    result = None

Prevention

When it happens

Trigger: calculate_resistance(4, ['Violet','Brown','Grey','Silver','Green']) — 5 colors for a 4-band resistor; forgetting that a 4-band code includes the tolerance band; dropping or adding a band when transcribing a code.

Common situations: Transcribing resistor codes by hand and missing the tolerance band; parsing banded strings and splitting incorrectly; assuming the multiplier band is not part of the count.

Related errors


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