{"record":{"id":"fb82b7a77fe9d227","repo":"TheAlgorithms/Python","slug":"expecting-number-of-bands-colors-provided-len","errorCode":null,"errorMessage":"Expecting {number_of_bands} colors, provided {len(colors)} colors","messagePattern":"Expecting (.+?) colors, provided (.+?) colors","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"electronics/resistor_color_code.py","lineNumber":289,"sourceCode":"      ...\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\n    >>> calculate_resistance(4, [\"Orange\",\"Green\",\"Blue\",\"Gold\"])\n    {'resistance': '35000000Ω ±5% '}\n\n    >>> calculate_resistance(5, [\"Violet\",\"Brown\",\"Grey\",\"Silver\",\"Green\"])","sourceCodeStart":271,"sourceCodeEnd":307,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/electronics/resistor_color_code.py#L271-L307","documentation":"Raised by check_validity() in electronics/resistor_color_code.py when len(colors) differs from number_of_bands (after number_of_bands has already passed the 3-6 range check). A resistor's color list must contain exactly one color per band: 3 bands means 3 colors, 4 means 4, etc.","triggerScenarios":"calculate_resistance(4, ['Violet','Brown','Grey','Silver','Green']) — 5 colors for a 4-band resistor; forgetting that a 4-band code includes the tolerance band; dropping or adding a band when transcribing a code.","commonSituations":"Transcribing resistor codes by hand and missing the tolerance band; parsing banded strings and splitting incorrectly; assuming the multiplier band is not part of the count.","solutions":["Count the resistor's physical bands first and pass exactly that many colors, in band order (significant digits, multiplier, tolerance, temp coefficient).","Validate len(colors) == number_of_bands before calling check_validity or calculate_resistance.","For a 4-band resistor remember the layout is digit-digit-multiplier-tolerance (4 colors), not 3."],"exampleFix":"# before\ncalculate_resistance(4, [\"Violet\", \"Brown\", \"Grey\", \"Silver\", \"Green\"])  # 5 colors\n\n# after\ncalculate_resistance(5, [\"Violet\", \"Brown\", \"Grey\", \"Silver\", \"Green\"])  # 5 bands, 5 colors","handlingStrategy":"validation","validationCode":"if len(color_code_list) != number_of_bands:\n    raise UserInputError(\n        f\"Need exactly {number_of_bands} colors, got {len(color_code_list)}\"\n    )","typeGuard":null,"tryCatchPattern":"try:\n    result = calculate_resistance(number_of_bands, color_code_list)\nexcept ValueError as exc:\n    logger.warning(\"Resistor input rejected: %s\", exc)\n    result = None","preventionTips":["Derive number_of_bands from len(color_code_list) instead of passing both independently.","Remember band layout: N bands means exactly N colors including tolerance and temp-coefficient bands.","Lint user input lengths in forms before submission."],"tags":["electronics","resistor","validation","arity"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}