Complete the code to encrypt a message by shifting each letter by 3 positions.
def encrypt(message): result = "" for char in message: if char.isalpha(): shifted = chr((ord(char) - 65 + [1]) % 26 + 65) result += shifted else: result += char return result
Shifting letters by 3 positions is a common simple encryption called Caesar cipher.
Complete the code to decrypt a message encrypted by shifting letters by 3 positions.
def decrypt(message): result = "" for char in message: if char.isalpha(): shifted = chr((ord(char) - 65 - [1]) % 26 + 65) result += shifted else: result += char return result
Decrypting reverses the encryption by shifting letters backward by the same number.
Fix the error in the code to correctly check if a character is uppercase before encrypting.
def encrypt(message): result = "" for char in message: if [1]: shifted = chr((ord(char) - 65 + 3) % 26 + 65) result += shifted else: result += char return result
We only want to shift uppercase letters, so we check if the character is uppercase.
Fill both blanks to create a dictionary comprehension that maps each letter to its encrypted form by shifting 2 positions.
encrypted_dict = {char: chr((ord(char) - 65 [1] 2) % 26 + 65) for char in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' if char [2] 'M'}The first blank needs '+' to shift letters forward by 2. The second blank uses '<' to filter letters before 'M'.
Fill all three blanks to create a dictionary comprehension that maps lowercase letters to uppercase encrypted letters shifted by 1, only for letters greater than 'f'.
encrypted_lower = { [1]: [2] for [3] in 'abcdefghijklmnopqrstuvwxyz' if [3] > 'f' }The dictionary comprehension syntax requires a key-value pair separated by ':'. The key is 'char', the value is the encrypted uppercase letter shifted by 1.