{"record":{"id":"29e88a269717d21e","repo":"TheAlgorithms/Python","slug":"encode-accepts-only-letters-of-the-alphabet-and","errorCode":null,"errorMessage":"encode() accepts only letters of the alphabet and spaces","messagePattern":"encode\\(\\) accepts only letters of the alphabet and spaces","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"ciphers/baconian_cipher.py","lineNumber":58,"sourceCode":"def encode(word: str) -> str:\n    \"\"\"\n    Encodes to Baconian cipher\n\n    >>> encode(\"hello\")\n    'AABBBAABAAABABAABABAABBAB'\n    >>> encode(\"hello world\")\n    'AABBBAABAAABABAABABAABBAB BABAAABBABBAAAAABABAAAABB'\n    >>> encode(\"hello world!\")\n    Traceback (most recent call last):\n        ...\n    Exception: encode() accepts only letters of the alphabet and spaces\n    \"\"\"\n    encoded = \"\"\n    for letter in word.lower():\n        if letter.isalpha() or letter == \" \":\n            encoded += encode_dict[letter]\n        else:\n            raise Exception(\"encode() accepts only letters of the alphabet and spaces\")\n    return encoded\n\n\ndef decode(coded: str) -> str:\n    \"\"\"\n    Decodes from Baconian cipher\n\n    >>> decode(\"AABBBAABAAABABAABABAABBAB BABAAABBABBAAAAABABAAAABB\")\n    'hello world'\n    >>> decode(\"AABBBAABAAABABAABABAABBAB\")\n    'hello'\n    >>> decode(\"AABBBAABAAABABAABABAABBAB BABAAABBABBAAAAABABAAAABB!\")\n    Traceback (most recent call last):\n        ...\n    Exception: decode() accepts only 'A', 'B' and spaces\n    \"\"\"\n    if set(coded) - {\"A\", \"B\", \" \"} != set():\n        raise Exception(\"decode() accepts only 'A', 'B' and spaces\")","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/ciphers/baconian_cipher.py#L40-L76","documentation":"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).","triggerScenarios":"Calling encode('hello world!') as in the doctest; also digits ('abc123') or any punctuation ('hi, there?'). Spaces are allowed — only a-z and ' ' pass.","commonSituations":"Encrypting raw sentences with punctuation or numbers, or assuming the function strips non-letters automatically like a1z26-style ciphers elsewhere in the repo.","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."],"exampleFix":"# before\nencode('hello world!')  # Exception: encode() accepts only letters of the alphabet and spaces\n\n# after\nencode(''.join(c for c in 'hello world!' if c.isalpha() or c == ' '))","handlingStrategy":"validation","validationCode":"sanitized = ''.join(c for c in text.lower() if c.isalpha() or c == ' ')\nif not sanitized:\n    raise ValueError('nothing to encode')","typeGuard":"def is_baconian_encodable(s: str) -> bool:\n    return all(c.isalpha() or c == ' ' for c in s.lower())","tryCatchPattern":"try:\n    encode(text)\nexcept Exception as e:  # note: bare Exception, not ValueError\n    if 'accepts only letters' in str(e):\n        encode(''.join(c for c in text.lower() if c.isalpha() or c == ' '))\n    else:\n        raise","preventionTips":["Strip punctuation/digits before Baconian encoding.","Catch bare Exception — this function does not raise ValueError."],"tags":["cipher","validation","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}