{"record":{"id":"ca4239b48fc960d9","repo":"TheAlgorithms/Python","slug":"third-rotor-position-is-not-within-range-of-1-26","errorCode":null,"errorMessage":"Third rotor position is not within range of 1..26 ({rotorpos3}","messagePattern":"Third rotor position is not within range of 1\\.\\.26 \\((.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"ciphers/enigma_machine2.py","lineNumber":106,"sourceCode":"    :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'}\n\n    In the code, ``pb`` stands for ``plugboard``\n","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/ciphers/enigma_machine2.py#L88-L124","documentation":"Raised by the Enigma machine validator when the THIRD rotor position is outside 1..26. Identical rule to the other two rotor checks; this message's f-string is correctly closed.","triggerScenarios":"rotpos=(1, 1, 0), (26, 26, 27), or any third element < 1 or > 26.","commonSituations":"Loop-generated settings where the last rotor got a boundary value; typos in hand-written triplets; mixing 0-based indexing in generated test cases.","solutions":["Keep the third position in 1..26, e.g. rotpos=(1, 13, 26)","Validate the full tuple before the call","Reject out-of-range settings at the UI/config layer"],"exampleFix":"# before\nrotpos = (1, 1, 99)\n\n# after\nrotpos = (1, 1, 26)","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":["Validate all three positions together in one pass","Treat 0 and 27+ as config errors early","Keep a canonical valid example like (1, 13, 26) for tests"],"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"}