0
0
Intro to Computingfundamentals~10 mins

Encryption basics in Intro to Computing - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to encrypt a message by shifting each letter by 3 positions.

Intro to Computing
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
Drag options to blanks, or click blank then click option'
A0
B1
C5
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 means no encryption happens.
Using 1 or 5 shifts letters incorrectly for this example.
2fill in blank
medium

Complete the code to decrypt a message encrypted by shifting letters by 3 positions.

Intro to Computing
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
Drag options to blanks, or click blank then click option'
A3
B5
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 means no change, so message stays encrypted.
Using a different number than encryption causes wrong output.
3fill in blank
hard

Fix the error in the code to correctly check if a character is uppercase before encrypting.

Intro to Computing
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
Drag options to blanks, or click blank then click option'
Achar.isalpha()
Bchar.isupper()
Cchar.isdigit()
Dchar.islower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using isalpha() includes lowercase letters which may cause wrong shifts.
Using isdigit() or islower() is incorrect for uppercase check.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each letter to its encrypted form by shifting 2 positions.

Intro to Computing
encrypted_dict = {char: chr((ord(char) - 65 [1] 2) % 26 + 65) for char in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' if char [2] 'M'}
Drag options to blanks, or click blank then click option'
A+
B-
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' in the first blank shifts letters backward.
Using '>' in the second blank selects letters after 'M'.
5fill in blank
hard

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'.

Intro to Computing
encrypted_lower = { [1]: [2] for [3] in 'abcdefghijklmnopqrstuvwxyz' if [3] > 'f' }
Drag options to blanks, or click blank then click option'
A{char.upper()}
Bchar.upper()
Cchar
Dchr((ord(char.upper()) - 65 + 1) % 26 + 65)
Attempts:
3 left
💡 Hint
Common Mistakes
Using string interpolation in the key instead of a variable.
Not using the correct dictionary comprehension syntax.