TheAlgorithms/Python · error · ValueError

{color} is not a valid color for temperature coeffecient ban

Error message

{color} is not a valid color for temperature coeffecient band

What it means

Thrown by get_temperature_coeffecicient() in electronics/resistor_color_code.py when color is not a key of temperature_coeffecient_color_values. The temperature-coefficient (6th) band has its own small color set (e.g. Brown 100, Red 50, Orange 15, Yellow 25, Blue 10, Violet 5 per the library table); colors from other band types (Cyan, Green-as-tolerance) are rejected.

Source

Thrown at electronics/resistor_color_code.py:217


def get_temperature_coeffecient(color: str) -> int:
    """
    Function returns the temperature coeffecient value associated with the color.
    Function takes color as input and returns temperature coeffecient value.

    >>> get_temperature_coeffecient('Yellow')
    25

    >>> get_temperature_coeffecient('Cyan')
    Traceback (most recent call last):
      ...
    ValueError: Cyan is not a valid color for temperature coeffecient band

    """
    if color not in temperature_coeffecient_color_values:
        msg = f"{color} is not a valid color for temperature coeffecient band"
        raise ValueError(msg)
    return temperature_coeffecient_color_values[color]


def get_band_type_count(total_number_of_bands: int, type_of_band: str) -> int:
    """
    Function returns the number of bands of a given type in a resistor with n bands
    Function takes total_number_of_bands and type_of_band as input and returns
    number of bands belonging to that type in the given resistor

    >>> get_band_type_count(3,'significant')
    2

    >>> get_band_type_count(2,'significant')
    Traceback (most recent call last):
      ...
    ValueError: 2 is not a valid number of bands

    >>> get_band_type_count(3,'sign')

View on GitHub (pinned to f5988cc097)

Solutions

  1. Use a temp-coeff color from the table: get_temperature_coeffecient('Yellow') -> 25.
  2. Verify the resistor actually has 6 bands before calling (see get_band_type_count).
  3. Check band order against the library's documented layout and normalize color-name casing.

Example fix

# before
get_temperature_coeffecient('Cyan')  # ValueError

# after
get_temperature_coeffecient('Yellow')  # 25
Defensive patterns

Strategy: type-guard

Validate before calling

if color not in temperature_coeffecient_color_values:
    raise ValueError(f'{color!r} is not a temp-coefficient color')

Type guard

def is_temp_coeff_color(color: str) -> bool:
    return color in temperature_coeffecient_color_values

Try / catch

try:
    tc = get_temperature_coeffecient(color)
except ValueError as exc:
    # only 6-band resistors have this band; verify count first
    ...

Prevention

When it happens

Trigger: get_temperature_coeffecient('Cyan'); passing a 4/5-band resistor color into the 6-band function; using a color whose name casing differs from the table key.

Common situations: Decoding a 6-band resistor but reading the wrong physical band; assuming all colors are valid for every band type; copy-pasting color lists between get_tolerance and get_temperature_coeffecicient calls.

Related errors


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