{"record":{"id":"b05a0fdb5f0e6c7b","repo":"TheAlgorithms/Python","slug":"each-message-character-has-to-be-included-in-alpha","errorCode":null,"errorMessage":"Each message character has to be included in alphabet!","messagePattern":"Each message character has to be included in alphabet!","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"ciphers/trifid_cipher.py","lineNumber":113,"sourceCode":"        ...\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\n\r\n    Encrypts a message using the trifid_cipher. Any punctuatuion chars that\r","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/ciphers/trifid_cipher.py#L95-L131","documentation":"Raised by trifid_cipher's __prepare helper when any character of the (space-stripped, uppercased) message is not present in the supplied alphabet. The trifid cipher can only encode symbols that exist in its 27-character alphabet, so foreign characters make the number-substitution step impossible.","triggerScenarios":"Message containing digits or punctuation not in the alphabet: encrypt_message('MEET AT 9PM') (the '9' is not in the default alphabet), or a custom alphabet that omits letters used in the message, e.g. alphabet='ABCDEFGHIJKLMNOQRSTUVWXY.Z' with message 'HELP'.","commonSituations":"Passing user-typed text with digits, commas, or question marks; using a custom alphabet that drops letters the message still uses; forgetting that spaces are removed automatically but other whitespace/punctuation is not.","solutions":["Sanitize the message to A-Z plus '.' before calling: re.sub(r'[^A-Z.]', '', message.upper())","Or extend the alphabet with the missing characters while keeping its length at 27","Check the error occurs after the length check, so fix any alphabet-length error (error 80) first"],"exampleFix":"# before\nencrypt_message('ROOM 12', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.')\n# ValueError: Each message character has to be included in alphabet!\n\n# after\nimport re\nencrypt_message(re.sub(r'[^A-Z.]', '', 'ROOM 12'), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.')","handlingStrategy":"validation","validationCode":"import re\nclean = re.sub(r'[^A-Z.]', '', message.upper())\n# then pass `clean` and a 27-char alphabet covering it","typeGuard":"def message_in_alphabet(message: str, alphabet: str) -> bool:\n    a = set(alphabet.replace(' ', '').upper())\n    return all(ch in a for ch in message.replace(' ', '').upper())","tryCatchPattern":"try:\n    encrypt_message(msg, alphabet)\nexcept ValueError as e:\n    if 'included in alphabet' in str(e):\n        msg = ''.join(c for c in msg.upper() if c in alphabet)\n        return encrypt_message(msg, alphabet)\n    raise","preventionTips":["Uppercase and strip non-alphabet characters before encrypting","Keep a single canonical 27-char alphabet constant shared across calls","Reject mixed-source text (digits, punctuation) at input parsing"],"tags":["ciphers","trifid","validation","input-sanitization"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}