{"record":{"id":"9033706807d4c767","repo":"TheAlgorithms/Python","slug":"second-rotor-position-is-not-within-range-of-1-26","errorCode":null,"errorMessage":"Second rotor position is not within range of 1..26 ({rotorpos2}","messagePattern":"Second rotor position is not within range of 1\\.\\.26 \\((.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"ciphers/enigma_machine2.py","lineNumber":103,"sourceCode":"    :param rotpos: rotor_positon\n    :param rotsel: rotor_selection\n    :param pb: plugb -> validated and transformed\n    :return: (`rotpos`, `rotsel`, `pb`)\n    \"\"\"\n    # Checks if there are 3 unique rotors\n\n    if (unique_rotsel := len(set(rotsel))) < 3:\n        msg = f\"Please use 3 unique rotors (not {unique_rotsel})\"\n        raise Exception(msg)\n\n    # Checks if rotor positions are valid\n    rotorpos1, rotorpos2, rotorpos3 = rotpos\n    if not 0 < rotorpos1 <= len(abc):\n        msg = f\"First rotor position is not within range of 1..26 ({rotorpos1}\"\n        raise ValueError(msg)\n    if not 0 < rotorpos2 <= len(abc):\n        msg = f\"Second rotor position is not within range of 1..26 ({rotorpos2})\"\n        raise ValueError(msg)\n    if not 0 < rotorpos3 <= len(abc):\n        msg = f\"Third rotor position is not within range of 1..26 ({rotorpos3})\"\n        raise ValueError(msg)\n\n    # Validates string and returns dict\n    pbdict = _plugboard(pb)\n\n    return rotpos, rotsel, pbdict\n\n\ndef _plugboard(pbstring: str) -> dict[str, str]:\n    \"\"\"\n    https://en.wikipedia.org/wiki/Enigma_machine#Plugboard\n\n    >>> _plugboard('PICTURES')\n    {'P': 'I', 'I': 'P', 'C': 'T', 'T': 'C', 'U': 'R', 'R': 'U', 'E': 'S', 'S': 'E'}\n    >>> _plugboard('POLAND')\n    {'P': 'O', 'O': 'P', 'L': 'A', 'A': 'L', 'N': 'D', 'D': 'N'}","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/ciphers/enigma_machine2.py#L85-L121","documentation":"Raised by the Enigma machine validator when the SECOND rotor position is outside 1..26. Same 1-based rule as the other rotors; the message f-string also misses its closing parenthesis (cosmetic bug).","triggerScenarios":"rotpos=(1, 0, 1), (5, 27, 5), or any tuple whose second element is < 1 or > 26.","commonSituations":"Building rotor settings from user input where the middle value was mistyped; arrays indexed from 0; settings files copied from an implementation with 0-based positions.","solutions":["Set the second position within 1..26, e.g. rotpos=(1, 13, 26)","Range-check the whole tuple up front: assert all(0 < p <= 26 for p in rotpos)","Parse and clamp user-supplied settings before passing them in"],"exampleFix":"# before\nrotpos = (1, 0, 1)\n\n# after\nrotpos = (1, 1, 1)","handlingStrategy":"validation","validationCode":"assert all(0 < p <= 26 for p in rotpos), \"positions must be in 1..26\"","typeGuard":"def valid_rotor_positions(rotpos) -> bool:\n    return len(rotpos) == 3 and all(0 < p <= 26 for p in rotpos)","tryCatchPattern":"try:\n    settings = validate_and_transform(rotpos, rotsel, pb)\nexcept ValueError as exc:\n    if \"rotor position\" in str(exc):\n        raise ConfigError(\"rotor settings out of range 1..26\") from exc\n    raise","preventionTips":["Range-check every element of the position tuple before the call","Convert 0-based external settings with p + 1","Reject bad settings at your config/UI layer with a clear message"],"tags":["enigma","rotor-position","off-by-one","validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}