WRITE EVERY QUESTION SHORT SIMPLE AND INFORMAL WITH NO AI AS HUMAN WRITTEN Practice answer the following questions and complete the activity:

1. What are some improvements that can be made to our program so that it is even more useful.
2. What are some challenges that could follow from adding this additional functionality?
3. What types of things do you need to be aware of when testing new functionality?
4. Activity: Using the encoding process as a model, create a function that will decipher an encrypted into a plaintext message or encrypt a plaintext message into a ciphertext string.

Second briefly one or two sentences answer question 1-3 from the assignment below are some thoughts for answering each question:
Question 1: Could the program be coded so that a given letter isn't always the same encrypted letter? (Ex. 'A' isn't always encrypted to 'E')?
Question 2: Are there challenges in coding when sometimes 'A' is an 'E' and sometimes 'A' is a different letter? Do you think writing code for 'A' to be any random letter is possible?
Question 3: As a soon to be programmer (We'll start writing our own code in Unit 4), you'll find out that you always need to be aware of whether your program is producing the correct output. Why do you think correct output is important? You can use an example to explain your answer. My example is that I am glad that when I choose a song from my playlist to listen to, that I will hear the song I chose and not some other song that I didn't want to hear.

def encrypt(message):
ciphertext = ""
for x in message:
number = ord(x)# convert the letter to a numeric value
number = number - ord('A') #subtract the unicode for A
number = (number + 4) # add the key
number = number % 26 # find the remainder
number = number + ord('A') # add back the first letter
letter = chr(number)
ciphertext=ciphertext + letter
return (ciphertext)
message = "ZEBRA"
print(encrypt(message))

1 answer

Sure! Here are the answers and the activity:

  1. Improvements: We could make the program so that each letter gets a different encrypted letter every time (like a random substitution).

  2. Challenges: Coding for random substitutions can be tough because it’s tricky to keep track of which letter was changed to which. It might also mess up the decryption process.

  3. Testing Awareness: You always gotta check if the output is correct because if it isn’t, you’ll get unexpected results. Like if you order a pizza and get a salad instead, that's not what you wanted!

Activity Code:

Here’s a simple function that can either encrypt or decrypt a message:

import random

def encrypt(message, key):
    ciphertext = ""
    for x in message:
        number = ord(x)
        number = number - ord('A')
        number = (number + key) % 26
        number = number + ord('A')
        letter = chr(number)
        ciphertext += letter
    return ciphertext

def decrypt(ciphertext, key):
    plaintext = ""
    for x in ciphertext:
        number = ord(x)
        number = number - ord('A')
        number = (number - key) % 26
        number = number + ord('A')
        letter = chr(number)
        plaintext += letter
    return plaintext

key = random.randint(1, 25)  # Random key between 1 and 25
message = "ZEBRA"
encrypted_message = encrypt(message, key)
decrypted_message = decrypt(encrypted_message, key)

print(f"Encrypted: {encrypted_message}")
print(f"Decrypted: {decrypted_message}")

This code generates a random key for the encryption and decryption process!