memoryc = open("cipher.txt", "a")
memoryd = open("decipher.txt", "a")
#-------------------------------------------------------------------------------
choice = input("cipher, decipher or memory")
#-------------------------------------------------------------------------------
if choice == "cipher":
    message = input("What is your message?")
    key = int(input("What is your key?"))
    coded_message = ""
    for ch in message:
        code_val  = ord(ch) + key
        if ch.isalpha():
            if code_val > ord('z'):
                code_val -= ord('z') - ord('a')
            coded_message = coded_message + chr(code_val)
        else:
            coded_message = coded_message + ch
    memoryc.write(coded_message +"\n")
    print(coded_message)
    quit = input("Do you want to quit")
#-------------------------------------------------------------------------------
if choice == "decipher":
    message = input("What is your message?")
    key = int(input("What is your key?"))
    coded_message = ""
    for ch in message:
        code_val  = ord(ch) - key
        if ch.isalpha():
            if code_val > ord('z'):
                code_val -= ord('z') + ord('a')
            coded_message = coded_message + chr(code_val)
        else:
            coded_message = coded_message + ch
    memoryd.write(coded_message +"\n")
    print(coded_message)
    quit = input("Do you want to quit")
#-------------------------------------------------------------------------------
if choice == "memory":
    cd = input("cipher or decipher memory?")
    if cd == "cipher":
        memoryc = open("cipher.txt", "r")
        ciphermemory = memoryc.read()
        print(ciphermemory)
    if cd == "decipher":
        memoryd = open("decipher.txt", "r")
        deciphermemory = memoryd.read()
        print(deciphermemory)
#-------------------------------------------------------------------------------
memoryc.close()
memoryd.close()

when using the decipher part of my program it doesn't work correctly with all letters. Any help appretiated.

Recommended Answers

Use unichr instead of chr

Jump to Post

Yep, note that I skimmed through your code and didn't run it on my machine. So hopefully my initial thoughts fix your problem.

Jump to Post

All 5 Replies

Use unichr instead of chr

So just replace all the ch with unichr

Yep, note that I skimmed through your code and didn't run it on my machine. So hopefully my initial thoughts fix your problem.

Changing chr() to unichr() won't help. your algorithm is wrong!

To see where you went wrong take a looky at:
inventwithpython.com/chapter14.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.