Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Caeser Cypher Help
Back story: I was asked to teach some technology classes this year. Little did I know I would be asked to teach python. Now I am learning python with the kids. One of their assignments is to write a script that inputs a line of plaintext and a distance value and outputs an encrypted text using a Caesar cipher. The script should work for any printable characters. The code I wrote works well except it does not work for characters. Can anyone help me tell me what I am doing incorrectly?
# Caesar Cipher


def getMode():
    while True:
       print('Do you wish to encrypt or decrypt a message?')
       mode = input().lower()
       if mode in 'encrypt e decrypt d'.split():
           return mode
           print('Enter either "encrypt" or "e" or "decrypt" or "d".')
def getMessage():
    print('Enter your message:')
    return input()

def getKey():
    key = 0
    while True:
        print('Enter the key number (1-%s)' % (MAX_KEY_SIZE))
        key = int(input())
        if (key >= 1 and key <= MAX_KEY_SIZE):
            return key
def getTranslatedMessage(mode, message, key):
    if mode[0] == 'd':
        key = -key
    translated = ''
    for symbol in message:
        if symbol.isalpha():
            num = ord(symbol)
            num += key
            if symbol.isupper():
                if num > ord('Z'):
                    num -= 26
                elif num < ord('A'):
                    num += 26
            elif symbol.islower():
                if num > ord('z'):
                    num -= 26
                elif num < ord('a'):
                    num += 26
            translated += chr(num)
           translated += symbol
    return translated

mode = getMode()
message = getMessage()
key = getKey()

print('Your translated text is:')
print(getTranslatedMessage(mode, message, key))

buran wrote Nov-11-2019, 06:46 AM:
removed crossing of text as it seems done in error
Your code seems to work, so I'm assuming you crossed out all of your text because you didn't really want to ask a question anymore.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Thanks for replying. It works for everything except characters. I didn't mean to strikeout the text. I had a case of forgetfulness yesterday. :(
What do you mean by characters? To me, every letter, number, punctuation, and other symbol is a character. Can you show me exactly what you are trying to encode and how it isn't encoding correctly?
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Sorry, I mean it won't change numbers and other symbols. It changes every letter Thanks for your help.
What would you change them to? The Caesar Cipher is traditionally defined only for letters, so it's not defined for numbers and symbols. Currently your code handles upper and lower case letters separately. Would you want to handle numbers and symbols as separate batches? If you would need to add sections like the if symbol.isalpha(): on line 32. But there is a problem with the non-alphanumeric characters. You are using ord with modular arithmetic to do the transformation, which is implicitly using ASCII. However, the non-alphanumeric characters are not all together on the ASCII table, so you would have to break them in to separate batches or doing something else to make that work.

The other option would be to run it on the full ASCII table, from 32 to 126. But then you would loose the standard handling of the letters. For example, if your key was 3, 'Z' would go to ']' instead of going to 'C'.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures


Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  The code to decrypt Caeser Cipher. lazerwolf101 2 186 May-26-2020, 04:01 PM
Last Post: DT2000
  Caesar Cypher--- I don't understand why it doesn't work ironsheep 12 1,490 Nov-03-2018, 06:53 PM
Last Post: j.crater

Forum Jump:

Users browsing this thread: 1 Guest(s)