# def encrypt_letter(letter):
#     ''' (str) -> str
#     
#     Precondition: len(letter) == 1 and
#     letter.isupper()
#     
#     Return letter encrypted by shifting
#     3 places to the right.
#     
#     >>> encrypt_letter('V')
#     'Y'
#     '''
#     ord_diff = ord(letter) - ord('A')
#     new_char_ord = (ord_diff + 3) % 26
#     return char(new_char_ord + ord('A'))

# make changes so that the function can be used to shift by n places
# where n a non-negative integer less than 26, instead of by 3 places.
# (4 MARKS)

def encrypt_letter(letter, n):
    ''' (str) -> str
    
    Precondition: len(letter) == 1, letter.isupper() and 0 <= n <= 26
    
    Return letter encrypted by shifting
    n places to the right.
    
    >>> encrypt_letter('V', 1)
    'W'
    '''
    ord_diff = ord(letter) - ord('A')
    new_char_ord = (ord_diff + n) % 26
    return char(new_char_ord + ord('A'))
    
def encrypt_message(plaintext):
    # complete the function body (5 MARKS)
    ''' (str) -> str
    
    Precondition: plaintext contains only letters (i.e. alphabetic characters)
    and single spaces.
    
    Return the ciphertext generated by reversing each word in plaintext.
    
    >>> encrypt_message(!HELLO COMPUTER SCIENCE WORLD!)
    !OLLEH RETUPMOC ECNEICS DLROW!
    >>> encrypt_message(!Good Luck!)
    !dooG kcuL!
    '''
    
    new_string = '!'
    words = plaintext.strip('!').split()
    new_words = []
    for i in range(len(words)):
        new_word = ''
        for j in range(len(words[i])):
            new_word += words[i][len(words[i]) - j - 1]
        new_words.append(new_word)
    for i in range(len(new_words)):
        new_string += new_words[i]
        if i < len(new_words) - 1:
            new_string += ' '
    new_string += '!'
    return new_string
    
def encrypt_message_sol(plaintext):
    ''' (str) -> str
    
    Precondition: plaintext contains only letters (i.e. alphabetic characters)
    and single spaces.
    
    Return the ciphertext generated by reversing each word in plaintext.
    
    >>> encrypt_message_sol('HELLO COMPUTER SCIENCE WORLD')
    '<NAME>'
    >>> encrypt_message_sol('Good Luck')
    'dooG kcuL'
    '''
    words = plaintext.split()
    ciphertext = ''
    for word in words:
        reversed = ''
        for letter in word:
            reversed = letter + reversed
        ciphertext += reversed + ' '
    return ciphertext.strip()
