#Activity 13.2.1 Solution - caesar cipher encoder

def caesar_cipher_encrypt(message, key):
    # A list of the characters that can be encrypted.
    # If a character other than one of these is used in a message it won't be encrypted.
    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']
    # A variable of type integer used to store the length of the list characters.
    numberInList = len(characters)

    # A variable initialised as an empty string and used to hold the encrypted message.
    encryptedMessage = ""

    # A 'for loop' that iterates through the message to be encrypted, character by character.
    for nextCharacter in message:

        # A boolean variable that acts as a flag, indicating if nextCharacter is in the list of characters.
        inList = False
        
        # An integer variable used in the condition for the 'while loop'.
        index = 0
        
        # A 'while loop' used to iterate through the list of characters looking for nextCharacter.
        while index < numberInList:
            newPosition = 0

            # An 'if statement' that encrypts next_character if it appears in the list of letters.
            if nextCharacter == characters[index]:  
               
                # Flag set to True if nextCharacter appears in the list
                inList = True
                
                # Encrypted character is derived by adding key to the character's current position in list.
                newPosition = index + key

                # Checks to see if a wrap around is needed e.g if the next character is 'w' and the key is = 5, the encrypted character will be 'B'.
                if newPosition >= numberInList:
                    newPosition -= numberInList
                encryptedChar = characters[newPosition]

                 
           # Increments the loop counter.
            index += 1

       # Adds encrypted character to the encrypted message.
        if inList:
             encryptedMessage += encryptedChar

        # If the character can't be encrypted it is simply added to the encrypted message.
        else:
            encryptedMessage += nextCharacter
    return encryptedMessage

   
#  Main program
message = input('Enter the message to encrypt: ')
key = int(input('Enter the key: '))
encrypted = caesar_cipher_encrypt(message, key)
print (encrypted)

