TheAlgorithms/Python · error · ValueError
Each message character has to be included in alphabet!
Error message
Each message character has to be included in alphabet!
What it means
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.
Source
Thrown at ciphers/trifid_cipher.py:113
...
ValueError: Each message character has to be included in alphabet!
Testing with numbers
>>> __prepare(500,'abCdeFghijkLmnopqrStuVwxYZ+')
Traceback (most recent call last):
...
AttributeError: 'int' object has no attribute 'replace'
"""
# Validate message and alphabet, set to upper and remove spaces
alphabet = alphabet.replace(" ", "").upper()
message = message.replace(" ", "").upper()
# Check length and characters
if len(alphabet) != 27:
raise KeyError("Length of alphabet has to be 27.")
if any(char not in alphabet for char in message):
raise ValueError("Each message character has to be included in alphabet!")
# Generate dictionares
character_to_number = dict(zip(alphabet, TEST_CHARACTER_TO_NUMBER.values()))
number_to_character = {
number: letter for letter, number in character_to_number.items()
}
return message, alphabet, character_to_number, number_to_character
def encrypt_message(
message: str, alphabet: str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.", period: int = 5
) -> str:
"""
encrypt_message
===============
Encrypts a message using the trifid_cipher. Any punctuatuion chars that
View on GitHub (pinned to f5988cc097)
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
Example fix
# before
encrypt_message('ROOM 12', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.')
# ValueError: Each message character has to be included in alphabet!
# after
import re
encrypt_message(re.sub(r'[^A-Z.]', '', 'ROOM 12'), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.') Defensive patterns
Strategy: validation
Validate before calling
import re clean = re.sub(r'[^A-Z.]', '', message.upper()) # then pass `clean` and a 27-char alphabet covering it
Type guard
def message_in_alphabet(message: str, alphabet: str) -> bool:
a = set(alphabet.replace(' ', '').upper())
return all(ch in a for ch in message.replace(' ', '').upper()) Try / catch
try:
encrypt_message(msg, alphabet)
except ValueError as e:
if 'included in alphabet' in str(e):
msg = ''.join(c for c in msg.upper() if c in alphabet)
return encrypt_message(msg, alphabet)
raise Prevention
- 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
When it happens
Trigger: 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'.
Common situations: 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.
Related errors
- Length of alphabet has to be 27.
- Non-binary value was passed to the function
- number must be positive
- The value of input must be non-negative
- Input list must contain at least two elements
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/b05a0fdb5f0e6c7b.
Report an issue: GitHub.