{"record":{"id":"d0d7e83d771475ff","repo":"TheAlgorithms/Python","slug":"odd-number-of-symbols-len-pbstring","errorCode":null,"errorMessage":"Odd number of symbols ({len(pbstring)})","messagePattern":"Odd number of symbols \\((.+?)\\)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"ciphers/enigma_machine2.py","lineNumber":139,"sourceCode":"    {'P': 'O', 'O': 'P', 'L': 'A', 'A': 'L', 'N': 'D', 'D': 'N'}\n\n    In the code, ``pb`` stands for ``plugboard``\n\n    Pairs can be separated by spaces\n\n    :param pbstring: string containing plugboard setting for the Enigma machine\n    :return: dictionary containing converted pairs\n    \"\"\"\n\n    # tests the input string if it\n    # a) is type string\n    # b) has even length (so pairs can be made)\n    if not isinstance(pbstring, str):\n        msg = f\"Plugboard setting isn't type string ({type(pbstring)})\"\n        raise TypeError(msg)\n    elif len(pbstring) % 2 != 0:\n        msg = f\"Odd number of symbols ({len(pbstring)})\"\n        raise Exception(msg)\n    elif pbstring == \"\":\n        return {}\n\n    pbstring.replace(\" \", \"\")\n\n    # Checks if all characters are unique\n    tmppbl = set()\n    for i in pbstring:\n        if i not in abc:\n            msg = f\"'{i}' not in list of symbols\"\n            raise Exception(msg)\n        elif i in tmppbl:\n            msg = f\"Duplicate symbol ({i})\"\n            raise Exception(msg)\n        else:\n            tmppbl.add(i)\n    del tmppbl\n","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/ciphers/enigma_machine2.py#L121-L157","documentation":"Raised by _plugboard() when the plugboard string has odd length: pairs cannot be formed. Every plugboard connection swaps two letters, so the string must contain an even number of symbols (spaces are not actually removed — see the no-op pbstring.replace call — so spaces count toward the length).","triggerScenarios":"_plugboard('PICTURE') with 7 letters; strings with a single stray space such as 'PICTURES ' (9 chars, odd); concatenated settings that dropped a character.","commonSituations":"Typing a pair twice or dropping one letter in config; strings containing separators the code fails to strip (the replace result is discarded); hand-editing historical settings like 'AM FI EV ...' and leaving an unpaired trailing space/letter.","solutions":["Supply an even-length string of complete pairs, e.g. 'PICTURES'","Strip spaces yourself first: pb = pb.replace(' ', '') before calling","Check len(pb) % 2 == 0 in your config validation"],"exampleFix":"# before\npb = 'PICTURES '  # 9 symbols after (broken) space handling\n\n# after\npb = 'PICTURES'.replace(' ', '')  # explicitly even length\n_plugboard(pb)","handlingStrategy":"validation","validationCode":"pb = pb.replace(' ', '')  # module's own strip is a no-op\nassert len(pb) % 2 == 0, \"plugboard needs complete pairs\"","typeGuard":"def has_paired_symbols(pb: str) -> bool:\n    return len(pb.replace(' ', '')) % 2 == 0","tryCatchPattern":"try:\n    pbdict = _plugboard(pb)\nexcept Exception as exc:  # module raises bare Exception\n    if \"Odd number\" in str(exc):\n        raise ConfigError('plugboard string must have even length') from exc\n    raise","preventionTips":["Strip spaces yourself — the module's pbstring.replace call discards its result","Always supply complete two-letter pairs","Validate even length after removing separators"],"tags":["enigma","plugboard","validation","odd-length"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}