TheAlgorithms/Python · error · ValueError

{total_number_of_bands} is not a valid number of bands

Error message

{total_number_of_bands} is not a valid number of bands

What it means

Thrown by get_band_type_count() in electronics/resistor_color_code.py when total_number_of_bands is not a key of the band_types dict — i.e. not one of the supported band counts (3, 4, 5, 6 per the table). A separate, second ValueError ('... is not valid for a N band resistor') fires when the count is valid but the band type is not present at that count; this one covers the count itself.

Source

Thrown at electronics/resistor_color_code.py:253

    >>> get_band_type_count(3,'sign')
    Traceback (most recent call last):
      ...
    ValueError: sign is not valid for a 3 band resistor

    >>> get_band_type_count(3,'tolerance')
    Traceback (most recent call last):
      ...
    ValueError: tolerance is not valid for a 3 band resistor

    >>> get_band_type_count(5,'temp_coeffecient')
    Traceback (most recent call last):
      ...
    ValueError: temp_coeffecient is not valid for a 5 band resistor

    """
    if total_number_of_bands not in band_types:
        msg = f"{total_number_of_bands} is not a valid number of bands"
        raise ValueError(msg)
    if type_of_band not in band_types[total_number_of_bands]:
        msg = f"{type_of_band} is not valid for a {total_number_of_bands} band resistor"
        raise ValueError(msg)
    return band_types[total_number_of_bands][type_of_band]


def check_validity(number_of_bands: int, colors: list) -> bool:
    """
    Function checks if the input provided is valid or not.
    Function takes number_of_bands and colors as input and returns
    True if it is valid

    >>> check_validity(3, ["Black","Blue","Orange"])
    True

    >>> check_validity(4, ["Black","Blue","Orange"])
    Traceback (most recent call last):
      ...

View on GitHub (pinned to f5988cc097)

Solutions

  1. Pass an int band count within the supported set: get_band_type_count(3, 'significant') -> 2.
  2. Coerce parsed values: int(total_number_of_bands) before calling.
  3. Verify the band count against the table keys (commonly band_types.keys()) or 3..6.

Example fix

# before
get_band_type_count(7, 'temp_coeffecient')  # ValueError: 7 is not a valid number of bands

# after
get_band_type_count(6, 'temp_coeffecient')  # 1
Defensive patterns

Strategy: validation

Validate before calling

VALID_BAND_COUNTS = {3, 4, 5, 6}
if not isinstance(total_number_of_bands, int) or total_number_of_bands not in VALID_BAND_COUNTS:
    raise ValueError('band count must be one of 3, 4, 5, 6')

Type guard

def is_valid_band_count(n: object) -> bool:
    return isinstance(n, int) and not isinstance(n, bool) and n in {3, 4, 5, 6}

Try / catch

try:
    n = get_band_type_count(bands, band_type)
except ValueError as exc:
    if 'not a valid number of bands' in str(exc):
        # recount bands / reject input
        ...
    elif 'not valid for a' in str(exc):
        # band type unsupported at this count
        ...
    raise

Prevention

When it happens

Trigger: get_band_type_count(2, 'significant'); get_band_type_count(7, 'temp_coeffecient'); get_band_type_count('4', 'significant') (string key never matches int keys in band_types).

Common situations: Miscounting bands on a small or oddly-marked resistor; passing a string digit from parsed input instead of int; assuming arbitrary band counts are supported when only 3-6 are.

Related errors


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