{"record":{"id":"8b88cb78561c5d8b","repo":"TheAlgorithms/Python","slug":"decode-accepts-only-a-b-and-spaces","errorCode":null,"errorMessage":"decode() accepts only 'A', 'B' and spaces","messagePattern":"decode\\(\\) accepts only 'A', 'B' and spaces","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"ciphers/baconian_cipher.py","lineNumber":76,"sourceCode":"            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\")\n    decoded = \"\"\n    for word in coded.split():\n        while len(word) != 0:\n            decoded += decode_dict[word[:5]]\n            word = word[5:]\n        decoded += \" \"\n    return decoded.strip()\n\n\nif __name__ == \"__main__\":\n    from doctest import testmod\n\n    testmod()\n","sourceCodeStart":58,"sourceCodeEnd":90,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/ciphers/baconian_cipher.py#L58-L90","documentation":"Raised by baconian_cipher decode() when the input contains characters outside {'A', 'B', ' '}. Decoding splits on spaces and consumes each word in 5-character chunks looked up in decode_dict, so any other character (lowercase 'a'/'b', digits, '!', etc.) breaks the chunking and is rejected with a bare Exception.","triggerScenarios":"Calling decode('AABBBAABAAABABAABABAABBAB BABAAABBABBAAAAABABAAAABB!') as in the doctest; also lowercase input ('aabb...') since the check is case-sensitive, or a word whose length is not a multiple of 5 (which instead fails with a KeyError on decode_dict lookup).","commonSituations":"Decoding text that was lowercased by a transport layer, copy-pasted with stray punctuation, or Baconian strings with wrong letter case from a different encoder convention.","solutions":["Normalize input: coded.upper() (and strip invalid chars) before decode().","Validate: set(coded) <= {'A','B',' '} in your caller before invoking.","Ensure words are multiples of 5 characters; otherwise decode_dict[word[:5]] raises KeyError instead."],"exampleFix":"# before\ndecode('aabbb')  # Exception: decode() accepts only 'A', 'B' and spaces\n\n# after\ndecode('aabbb'.upper())  # 'h'","handlingStrategy":"validation","validationCode":"coded = coded.upper()\nif set(coded) - {'A', 'B', ' '}:\n    raise ValueError('invalid Baconian input')\n# also verify each word length % 5 == 0 to avoid KeyError","typeGuard":"def is_baconian_decodable(s: str) -> bool:\n    s = s.upper()\n    return set(s) <= {'A', 'B', ' '} and all(len(w) % 5 == 0 for w in s.split())","tryCatchPattern":"try:\n    decode(coded)\nexcept Exception as e:  # bare Exception\n    if \"accepts only\" in str(e):\n        decode(coded.upper().replace('a', 'A').replace('b', 'B'))\n    else:\n        raise","preventionTips":["Uppercase Baconian input before decoding — the charset check is case-sensitive.","Validate word lengths are multiples of 5 to avoid the downstream KeyError."],"tags":["cipher","validation","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}