{"record":{"id":"4867728906ca9886","repo":"TheAlgorithms/Python","slug":"color-is-not-a-valid-color","errorCode":null,"errorMessage":"{color} is not a valid color","messagePattern":"(.+?) is not a valid color","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"electronics/resistor_color_code.py","lineNumber":285,"sourceCode":"    True\n\n    >>> check_validity(4, [\"Black\",\"Blue\",\"Orange\"])\n    Traceback (most recent call last):\n      ...\n    ValueError: Expecting 4 colors, provided 3 colors\n\n    >>> check_validity(3, [\"Cyan\",\"Red\",\"Yellow\"])\n    Traceback (most recent call last):\n      ...\n    ValueError: Cyan is not a valid color\n\n    \"\"\"\n    if number_of_bands >= 3 and number_of_bands <= 6:\n        if number_of_bands == len(colors):\n            for color in colors:\n                if color not in valid_colors:\n                    msg = f\"{color} is not a valid color\"\n                    raise ValueError(msg)\n            return True\n        else:\n            msg = f\"Expecting {number_of_bands} colors, provided {len(colors)} colors\"\n            raise ValueError(msg)\n    else:\n        msg = \"Invalid number of bands. Resistor bands must be 3 to 6\"\n        raise ValueError(msg)\n\n\ndef calculate_resistance(number_of_bands: int, color_code_list: list) -> dict:\n    \"\"\"\n    Function calculates the total resistance of the resistor using the color codes.\n    Function takes number_of_bands, color_code_list as input and returns\n    resistance\n\n    >>> calculate_resistance(3, [\"Black\",\"Blue\",\"Orange\"])\n    {'resistance': '6000Ω ±20% '}\n","sourceCodeStart":267,"sourceCodeEnd":303,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/electronics/resistor_color_code.py#L267-L303","documentation":"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.","triggerScenarios":"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.","commonSituations":"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').","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."],"exampleFix":"# before\ncheck_validity(3, [\"cyan\", \"Red\", \"Yellow\"])  # ValueError\n\n# after\nfrom electronics.resistor_color_code import valid_colors\nfrom electronics.resistor_color_code import check_validity\ncolors = [\"cyan\", \"Red\", \"Yellow\"]\ncanonical = [c.capitalize() for c in colors]\nassert all(c in valid_colors for c in canonical)\ncheck_validity(3, canonical)","handlingStrategy":"validation","validationCode":"from electronics.resistor_color_code import valid_colors\n\ncanonical = [c.strip().capitalize() for c in colors]\nif not all(c in valid_colors for c in canonical):\n    bad = [c for c in canonical if c not in valid_colors]\n    raise UserInputError(f\"Unknown colors: {bad}; allowed: {sorted(valid_colors)}\")","typeGuard":"def are_valid_colors(colors: list[str]) -> bool:\n    return all(isinstance(c, str) and c in valid_colors for c in colors)","tryCatchPattern":"try:\n    check_validity(bands, colors)\nexcept ValueError as exc:\n    # reprompt with the allowed color list\n    show_error(str(exc), allowed=sorted(valid_colors))","preventionTips":["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."],"tags":["electronics","resistor","color-code","validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}