{"record":{"id":"a5eb93c4b95f0f11","repo":"TheAlgorithms/Python","slug":"invalid-number-of-bands-resistor-bands-must-be-3","errorCode":null,"errorMessage":"Invalid number of bands. Resistor bands must be 3 to 6","messagePattern":"Invalid number of bands\\. Resistor bands must be 3 to 6","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"electronics/resistor_color_code.py","lineNumber":292,"sourceCode":"    >>> 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\"])\n    {'resistance': '7.18Ω ±0.5% '}\n\n    >>> calculate_resistance(6, [\"Red\",\"Green\",\"Blue\",\"Yellow\",\"Orange\",\"Grey\"])","sourceCodeStart":274,"sourceCodeEnd":310,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/electronics/resistor_color_code.py#L274-L310","documentation":"Raised by check_validity() in electronics/resistor_color_code.py when number_of_bands falls outside the supported 3-6 range (the function explicitly tests `number_of_bands >= 3 and number_of_bands <= 6`). The library models only 3-, 4-, 5-, and 6-band resistors; note calculate_resistance() re-raises this same condition.","triggerScenarios":"check_validity(2, ['Red','Red']) or check_validity(7, [...]) — 2-band and 7-band codes are unsupported; passing 0 or a negative count; passing a count parsed from text that failed int conversion earlier.","commonSituations":"Handling legacy 2-band resistors (implicit multiplier) which this library does not model; off-by-one when computing band count from a list length; new band counts added after a library update.","solutions":["Restrict number_of_bands to one of 3, 4, 5, or 6.","For 2-band resistors, extend the color list with an explicit 'Black' multiplier band (and a tolerance) so it becomes a valid 3/4-band call.","Validate the range upstream (e.g. in a CLI argument parser with choices=[3,4,5,6]) before calling the library."],"exampleFix":"# before\ncheck_validity(2, [\"Red\", \"Red\"])  # ValueError\n\n# after\ncheck_validity(3, [\"Red\", \"Red\", \"Black\"])  # 2-band code normalized to 3 bands","handlingStrategy":"validation","validationCode":"SUPPORTED_BANDS = (3, 4, 5, 6)\nif number_of_bands not in SUPPORTED_BANDS:\n    raise UserInputError(f\"Band count must be one of {SUPPORTED_BANDS}\")","typeGuard":"def is_supported_band_count(n) -> bool:\n    return isinstance(n, int) and 3 <= n <= 6","tryCatchPattern":"try:\n    ok = check_validity(number_of_bands, colors)\nexcept ValueError as exc:\n    raise InvalidBandSpec(str(exc)) from exc","preventionTips":["Constrain CLI/API options to 3-6 with choices/enum at the boundary.","Normalize 2-band codes to 3 bands by appending 'Black' multiplier.","Compute band count from the physical component, not a guess."],"tags":["electronics","resistor","validation","range-check"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}