def caesar_cipher(message, key, action):
    characters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
               '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    numberInList = len(characters)
    outputMessage = ""
    for nextCharacter in message:
        inList = False
        index = 0
        while index < numberInList:
            newPosition = 0
            if nextCharacter == characters[index]:
                inList = True
                # What happens next determined by value of action.
                if action == 'E':
                    newPosition = index + key
                else:
                    newPosition = index - key                   
                if newPosition >= numberInList:
                    newPosition -= numberInList
                outputChar = characters[newPosition]
            index += 1
        if inList:
             outputMessage += outputChar
        else:
            outputMessage += nextCharacter
    return outputMessage

   
#Main program
print('CAESAR CIPHER')
print('\nOPTIONS')
print('')
flag = True
while flag:
    print('\n1. Encrypt a message')
    print('2. Decrypt a message')
    print('3. Exit the program')
    answer = input("Please select an option. ")
    if answer == '1':
        message = input('Enter the message you want to encrypt: ')
        key = input('Enter the key: ')
        action = 'E'
        print('\nThe encoded message is: ')
        print(caesar_cipher(message, int(key), action))
    elif answer == '2':
        message = input('Enter the message you want to decrypt: ')
        key = input('Enter the key: ')
        action = 'D'
        print('\nThe decoded message is: '),
        print(caesar_cipher(message, int(key), action))
    elif answer =='3':
        flag = False
print('Good bye')
