hi again i have been looking at the bacon cipher and i have made this

import re
dictionaryofcodes = {'A': '11111', 'B': '11110', 'C': '11101', 'D': '11100', 'E': '110111', 'F': '11010', 'G': '11001', 'H': '11000', 'I': '10111', 'J': '10110', 'K': '10101', 'L': '10100', 'M': '10011', 'N': '10010', 'O': '10001', 'P': '10000', 'Q': '01111', 'R': '01110', 'S': '01101', 'T': '01100', 'U': '01011', 'V': '01010', 'W': '01001', 'X': '01000', 'Y': '00111', 'Z': '00110'}
reversedDictOfCodes = {"11111": "A", '11110': 'B', '11101': 'C', '11100': 'D', '11011': 'E', '11010': 'F', '11001': 'G', '11000': 'H', '10111': 'I', '10110': 'J', '10101': 'K', '10100': 'L', '10011': 'M', '10010': 'N', '10001': 'O', '10000': 'P', '01111': 'Q', '01110': 'R', '01101': 'S', '01100': 'T', '01011': 'U', '01010': 'V', '01001': 'W', '01000': 'X', '00111': 'Y', '00110': 'Z'}

text = 'hello i love this'




def encodeToAB(paragraph):
  # Task: encode a paragraph into a string of A and B s.
  paragraph = dropAllNonAlphabeticCharacters(paragraph)
  paragraph = convertToUpperCase(paragraph)
  paragraph = substituteEveryLetterByItsCode(paragraph)
  return paragraph

def dropAllNonAlphabeticCharacters(paragraph):
  # we can use a regex for this task
  pattern = aRegexWhichMatchesGroupsOfNonAlphabeticCharacters()
  paragraph = pattern.sub("", paragraph)
  return paragraph

def convertToUpperCase(paragraph):
  # hey, I know how to do this!
  return paragraph.upper()

def substituteEveryLetterByItsCode(paragraph):
  L = []
  for letter in paragraph:
    L.append(getCodeForLetter(letter))
  return "".join(L)


def aRegexWhichMatchesGroupsOfNonAlphabeticCharacters():
  # to solve this task, I must know the re module
  return re.compile(r"[^a-zA-Z]+")

def getCodeForLetter(letter):
  return dictionaryofcodes[letter]


p = 'hello'
print(encodeToAB(p))
print(encodeToAB(text))

how can i decode it?

either in a separate program or in the same one?

Here is an example how to do this:

# encoding and decoding a cipher structure

def encode(text):
    text = "".join([c.upper() for c in text if c.isalpha() or c==" "]) 
    temp = ""
    for c in text:
        temp += code_dic[c]
    return temp

def decode(crypt):
    temp = ""
    # use groups of 5
    for x in range(0, len(crypt), 5):
        temp += rev_dic[crypt[x:x+5]]
    return temp    

def swap_dict(d):
    """swap dictionary key:value pairs using a generator expression"""
    return dict((v, k) for (k, v) in d.items())


code_dic = {
'A': '11111', 'B': '11110', 'C': '11101', 'D': '11100', 'E': '11011', 
'F': '11010', 'G': '11001', 'H': '11000', 'I': '10111', 'J': '10110', 
'K': '10101', 'L': '10100', 'M': '10011', 'N': '10010', 'O': '10001', 
'P': '10000', 'Q': '01111', 'R': '01110', 'S': '01101', 'T': '01100', 
'U': '01011', 'V': '01010', 'W': '01001', 'X': '01000', 'Y': '00111', 
'Z': '00110', ' ': '00000'}

rev_dic = swap_dict(code_dic)

text = "Python is great!"
crypt = encode(text)

print(crypt)

print('-'*40)

news = decode(crypt)

print(news)

Notice I added a space char to the dictionary to make the decoded text more readable. There was also a mistake at 'E'.

One word of advice, don't make your variable and function names too long (ala C#) or your code becomes rather hard to read!

This question has already been answered. Start a new discussion instead.