TheAlgorithms/Python · error · ValueError

{color} is not a valid color for tolerance band

Error message

{color} is not a valid color for tolerance band

What it means

Thrown by get_tolerance() in electronics/resistor_color_code.py when color is not a key of tolerance_color_values (typically Brown, Red, Green, Blue, Violet, Gold, Silver and similar per the library table). Tolerance bands use a different color set than digit or multiplier bands, so colors valid elsewhere (Indigo, Yellow for temp-coeff) are rejected here.

Source

Thrown at electronics/resistor_color_code.py:197


def get_tolerance(color: str) -> float:
    """
    Function returns the tolerance value associated with the color.
    Function takes color as input and returns tolerance value.

    >>> get_tolerance('Green')
    0.5

    >>> get_tolerance('Indigo')
    Traceback (most recent call last):
      ...
    ValueError: Indigo is not a valid color for tolerance band

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


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:

View on GitHub (pinned to f5988cc097)

Solutions

  1. Use a tolerance-band color: get_tolerance('Green') -> 0.5, get_tolerance('Gold') -> 5.0.
  2. Confirm you are reading the correct band position (last band on a 4/5-band resistor).
  3. Pre-validate against the library's tolerance_color_values keys and normalize casing.

Example fix

# before
get_tolerance('Indigo')  # ValueError

# after
get_tolerance('Green')  # 0.5
Defensive patterns

Strategy: type-guard

Validate before calling

if color not in tolerance_color_values:
    raise ValueError(f'{color!r} is not a tolerance-band color')

Type guard

def is_tolerance_color(color: str) -> bool:
    return color in tolerance_color_values

Try / catch

try:
    t = get_tolerance(color)
except ValueError as exc:
    # re-prompt for the correct tolerance-band color
    ...

Prevention

When it happens

Trigger: get_tolerance('Indigo'); passing a digit-band-only color like 'Yellow' (if absent from the tolerance table); passing the multiplier color when the tolerance color was intended.

Common situations: Mixing up band positions when decoding by hand; UI dropdowns listing all colors for every band; user-entered color names with wrong casing or non-standard variants.

Related errors


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