{"record":{"id":"cd1dfc56b83caec7","repo":"TheAlgorithms/Python","slug":"length-of-alphabet-has-to-be-27","errorCode":null,"errorMessage":"Length of alphabet has to be 27.","messagePattern":"Length of alphabet has to be 27\\.","errorType":"exception","errorClass":"KeyError","httpStatus":null,"severity":"error","filePath":"ciphers/trifid_cipher.py","lineNumber":111,"sourceCode":"    >>> __prepare('am i a boy?','abCdeFghijkLmnopqrStuVwxYZ+')\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: Each message character has to be included in alphabet!\r\n\r\n    Testing with numbers\r\n\r\n    >>> __prepare(500,'abCdeFghijkLmnopqrStuVwxYZ+')\r\n    Traceback (most recent call last):\r\n        ...\r\n    AttributeError: 'int' object has no attribute 'replace'\r\n    \"\"\"\r\n    # Validate message and alphabet, set to upper and remove spaces\r\n    alphabet = alphabet.replace(\" \", \"\").upper()\r\n    message = message.replace(\" \", \"\").upper()\r\n\r\n    # Check length and characters\r\n    if len(alphabet) != 27:\r\n        raise KeyError(\"Length of alphabet has to be 27.\")\r\n    if any(char not in alphabet for char in message):\r\n        raise ValueError(\"Each message character has to be included in alphabet!\")\r\n\r\n    # Generate dictionares\r\n    character_to_number = dict(zip(alphabet, TEST_CHARACTER_TO_NUMBER.values()))\r\n    number_to_character = {\r\n        number: letter for letter, number in character_to_number.items()\r\n    }\r\n\r\n    return message, alphabet, character_to_number, number_to_character\r\n\r\n\r\ndef encrypt_message(\r\n    message: str, alphabet: str = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ.\", period: int = 5\r\n) -> str:\r\n    \"\"\"\r\n    encrypt_message\r\n    ===============\r","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/ciphers/trifid_cipher.py#L93-L129","documentation":"Raised by trifid_cipher's internal __prepare helper when the supplied alphabet does not contain exactly 27 characters after spaces are stripped and it is uppercased. The trifid cipher needs a 27-symbol alphabet: the 26 letters A-Z plus one extra symbol (the default is '.'). It is raised as a KeyError, which is unusual for input validation but matches the module's existing convention.","triggerScenarios":"Calling encrypt_message/decrypt_message with alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZ' (26 chars, missing the 27th symbol), or with a 27+ char alphabet containing duplicates/spaces that still leaves the length wrong after normalization, e.g. encrypt_message('HELP', 'ABC.') or __prepare('MSG', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.+').","commonSituations":"Copy-pasting a plain 26-letter alphabet from another cipher (Caesar, Playfair) into the trifid API; forgetting the trailing '.' separator; assuming the alphabet param is optional padding-tolerant like in other cipher modules.","solutions":["Use the default alphabet by omitting the argument: encrypt_message(message)","Or pass exactly 27 unique uppercase characters, e.g. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.'","If building alphabets dynamically, assert len(set(alphabet)) == 27 before calling","Note the alphabet must be a str; passing an int causes AttributeError ('int' object has no attribute 'replace') before this check runs"],"exampleFix":"# before\nencrypt_message('HELP', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')\n# KeyError: Length of alphabet has to be 27.\n\n# after\nencrypt_message('HELP', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.')","handlingStrategy":"validation","validationCode":"def valid_trifid_alphabet(alphabet: str) -> bool:\n    a = alphabet.replace(' ', '').upper()\n    return len(a) == 27 and len(set(a)) == 27","typeGuard":"def is_trifid_alphabet(v) -> bool:\n    return isinstance(v, str) and len(v.replace(' ', '')) == 27","tryCatchPattern":"try:\n    encrypt_message(msg, alphabet)\nexcept KeyError as e:\n    if 'Length of alphabet' in str(e):\n        raise ValueError('trifid alphabet must be 27 unique characters') from e\n    raise","preventionTips":["Default to omitting the alphabet argument","Build alphabets programmatically: string.ascii_uppercase + '.'","Assert len == 27 in tests before invoking the cipher"],"tags":["ciphers","trifid","validation","alphabet"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}