TheAlgorithms/Python · error · Exception
encode() accepts only letters of the alphabet and spaces
Error message
encode() accepts only letters of the alphabet and spaces
What it means
Raised by baconian_cipher encode() when, after lowercasing, any character is neither a letter nor a space. The Baconian cipher substitutes each letter with a 5-symbol A/B group from encode_dict, so digits and punctuation have no mapping and are rejected with a generic Exception (not ValueError).
Source
Thrown at ciphers/baconian_cipher.py:58
def encode(word: str) -> str:
"""
Encodes to Baconian cipher
>>> encode("hello")
'AABBBAABAAABABAABABAABBAB'
>>> encode("hello world")
'AABBBAABAAABABAABABAABBAB BABAAABBABBAAAAABABAAAABB'
>>> encode("hello world!")
Traceback (most recent call last):
...
Exception: encode() accepts only letters of the alphabet and spaces
"""
encoded = ""
for letter in word.lower():
if letter.isalpha() or letter == " ":
encoded += encode_dict[letter]
else:
raise Exception("encode() accepts only letters of the alphabet and spaces")
return encoded
def decode(coded: str) -> str:
"""
Decodes from Baconian cipher
>>> decode("AABBBAABAAABABAABABAABBAB BABAAABBABBAAAAABABAAAABB")
'hello world'
>>> decode("AABBBAABAAABABAABABAABBAB")
'hello'
>>> decode("AABBBAABAAABABAABABAABBAB BABAAABBABBAAAAABABAAAABB!")
Traceback (most recent call last):
...
Exception: decode() accepts only 'A', 'B' and spaces
"""
if set(coded) - {"A", "B", " "} != set():
raise Exception("decode() accepts only 'A', 'B' and spaces")View on GitHub (pinned to f5988cc097)
Solutions
- Sanitize first: ''.join(c for c in text.lower() if c.isalpha() or c == ' ') then encode.
- Or strip to letters only: ''.join(c for c in text if c.isalpha()).
- Catch Exception (not just ValueError) if you wrap encode() — it raises bare Exception.
Example fix
# before
encode('hello world!') # Exception: encode() accepts only letters of the alphabet and spaces
# after
encode(''.join(c for c in 'hello world!' if c.isalpha() or c == ' ')) Defensive patterns
Strategy: validation
Validate before calling
sanitized = ''.join(c for c in text.lower() if c.isalpha() or c == ' ')
if not sanitized:
raise ValueError('nothing to encode') Type guard
def is_baconian_encodable(s: str) -> bool:
return all(c.isalpha() or c == ' ' for c in s.lower()) Try / catch
try:
encode(text)
except Exception as e: # note: bare Exception, not ValueError
if 'accepts only letters' in str(e):
encode(''.join(c for c in text.lower() if c.isalpha() or c == ' '))
else:
raise Prevention
- Strip punctuation/digits before Baconian encoding.
- Catch bare Exception — this function does not raise ValueError.
When it happens
Trigger: Calling encode('hello world!') as in the doctest; also digits ('abc123') or any punctuation ('hi, there?'). Spaces are allowed — only a-z and ' ' pass.
Common situations: Encrypting raw sentences with punctuation or numbers, or assuming the function strips non-letters automatically like a1z26-style ciphers elsewhere in the repo.
Related errors
- plain must contain only lowercase letters (a-z)
- plaintext is empty
- key is empty
- ciphertext is empty
- decode() accepts only 'A', 'B' and spaces
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/29e88a269717d21e.
Report an issue: GitHub.