TheAlgorithms/Python · error · ValueError
{color} is not a valid color
Error message
{color} is not a valid color What it means
Raised by check_validity() in electronics/resistor_color_code.py when one of the colors in the colors list is not in the module-level valid_colors collection. valid_colors enumerates the accepted resistor-band color names (Black, Brown, Red, Orange, Yellow, Green, Blue, Violet, Grey, White, Gold, Silver, plus temperature-coefficient colors for 6-band use). Matching is case-sensitive and exact.
Source
Thrown at electronics/resistor_color_code.py:285
True
>>> check_validity(4, ["Black","Blue","Orange"])
Traceback (most recent call last):
...
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% '}
View on GitHub (pinned to f5988cc097)
Solutions
- Pass exact color names with the casing used in valid_colors (e.g. 'Black', 'Blue', 'Orange').
- Normalize input before calling: title-case the string and verify membership in valid_colors (import it from electronics.resistor_color_code).
- Map common aliases (UK 'Grey' vs 'Gray', abbreviations) to canonical names before validation.
- Catch ValueError and reprompt the user with the list of allowed colors.
Example fix
# before check_validity(3, ["cyan", "Red", "Yellow"]) # ValueError # after from electronics.resistor_color_code import valid_colors from electronics.resistor_color_code import check_validity colors = ["cyan", "Red", "Yellow"] canonical = [c.capitalize() for c in colors] assert all(c in valid_colors for c in canonical) check_validity(3, canonical)
Defensive patterns
Strategy: validation
Validate before calling
from electronics.resistor_color_code import valid_colors
canonical = [c.strip().capitalize() for c in colors]
if not all(c in valid_colors for c in canonical):
bad = [c for c in canonical if c not in valid_colors]
raise UserInputError(f"Unknown colors: {bad}; allowed: {sorted(valid_colors)}") Type guard
def are_valid_colors(colors: list[str]) -> bool:
return all(isinstance(c, str) and c in valid_colors for c in colors) Try / catch
try:
check_validity(bands, colors)
except ValueError as exc:
# reprompt with the allowed color list
show_error(str(exc), allowed=sorted(valid_colors)) Prevention
- Canonicalize case and trim whitespace before validation.
- Offer a picker from valid_colors in UIs instead of free text.
- Map aliases (Gray->Grey) at your ingestion boundary.
When it happens
Trigger: check_validity(3, ['Cyan', 'Red', 'Yellow']) — Cyan is not a resistor band color; passing lowercase or abbreviated colors like 'blk', 'black', or 'GRY'; passing a hex color string.
Common situations: Colors parsed from free-text user input or scraped from a datasheet table; case mismatches ('black' vs 'Black'); colors valid for one position but not the global list (e.g. 'None').
Related errors
- {color} is not a valid color for significant figure bands
- {total_number_of_bands} is not a valid number of bands
- {color} is not a valid color for multiplier band
- {color} is not a valid color for tolerance band
- {color} is not a valid color for temperature coeffecient ban
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/4867728906ca9886.
Report an issue: GitHub.