Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Caeser Cypher Help
#1
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

MAX_KEY_SIZE = 26

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
       else:
           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)
        else:
           translated += symbol
    return translated

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

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


Messages In This Thread
Caeser Cypher Help - by Ararisvalerian - Nov-10-2019, 12:54 AM
RE: Caeser Cypher Help - by ichabod801 - Nov-10-2019, 02:33 AM
RE: Caeser Cypher Help - by Ararisvalerian - Nov-10-2019, 11:00 AM
RE: Caeser Cypher Help - by ichabod801 - Nov-10-2019, 01:37 PM
RE: Caeser Cypher Help - by Ararisvalerian - Nov-10-2019, 04:17 PM
RE: Caeser Cypher Help - by ichabod801 - Nov-10-2019, 04:58 PM

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

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020