TheAlgorithms/Python · error · ValueError

{color} is not a valid color for significant figure bands

Error message

{color} is not a valid color for significant figure bands

What it means

Thrown by get_significant_digits() in electronics/resistor_color_code.py when any color in the input list is not a key of significant_figures_color_values (Black, Brown, Red, Orange, Yellow, Green, Blue, Violet, Grey, White). The function concatenates the digit each color maps to; colors valid for other band types (Gold, Silver) or non-standard names (Aqua) are rejected here because significant-figure bands only use the ten digit colors.

Source

Thrown at electronics/resistor_color_code.py:156

def get_significant_digits(colors: list) -> str:
    """
    Function returns the digit associated with the color. Function takes a
    list containing colors as input and returns digits as string

    >>> get_significant_digits(['Black','Blue'])
    '06'

    >>> get_significant_digits(['Aqua','Blue'])
    Traceback (most recent call last):
      ...
    ValueError: Aqua is not a valid color for significant figure bands

    """
    digit = ""
    for color in colors:
        if color not in significant_figures_color_values:
            msg = f"{color} is not a valid color for significant figure bands"
            raise ValueError(msg)
        digit = digit + str(significant_figures_color_values[color])
    return str(digit)


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

    >>> get_multiplier('Gold')
    0.1

    >>> get_multiplier('Ivory')
    Traceback (most recent call last):
      ...
    ValueError: Ivory is not a valid color for multiplier band

    """

View on GitHub (pinned to f5988cc097)

Solutions

  1. Pass only digit-band colors from the standard set: get_significant_digits(['Brown','Black']) -> '10'.
  2. Slice off tolerance/multiplier bands before calling, e.g. colors[:2] for a 4-band resistor.
  3. Normalize user input to the exact capitalized names used by the library's color tables.

Example fix

# before
get_significant_digits(['Aqua', 'Blue'])  # ValueError

# after
get_significant_digits(['Green', 'Blue'])  # '56'
Defensive patterns

Strategy: type-guard

Validate before calling

SIGNIFICANT_COLORS = {'Black','Brown','Red','Orange','Yellow','Green','Blue','Violet','Grey','White'}
if not all(c in SIGNIFICANT_COLORS for c in colors):
    raise ValueError(f'invalid significant-figure color in {colors}')

Type guard

def are_significant_colors(colors: list[str]) -> bool:
    return all(c in significant_figures_color_values for c in colors)

Try / catch

try:
    digits = get_significant_digits(colors)
except ValueError as exc:
    # show the user which color was rejected: exc names it
    ...

Prevention

When it happens

Trigger: get_significant_digits(['Aqua','Blue']); get_significant_digits(['Gold','Black']) (Gold is multiplier/tolerance only); misspelled or lowercased names like 'blue' if the mapping is case-sensitive.

Common situations: Passing a full 4/5-band color list including the Gold/Silver tolerance band into the digits function; user-typed color names with different casing or spelling (Gray vs Grey); regional/HTML color names instead of the resistor standard.

Related errors


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