Question: What is a Caesar Cipher? If we wanted to print the letter ' I ' , in Python we would write that as: print (

What is a Caesar Cipher? If we wanted to print the letter 'I', in Python we would write that as:
print(fruit[3])
Strings are Really Numbers
At the beginning of the term, we learned that letters are actually interpreted as numbers. These numbers are derived by the ASCII table. As you will recall the a lower case a is actually assigned to 97. The lower case letter \( z \) is assigned to 122. The computer uses these numeric values in order to do comparisons. We are going to use them to formulate our cipher.
The Algorithm to Encrypt
Below is the algorithm we will use to write a program that will encrypt text entered by the user:
```
For index =0 to length of string, step 1
Assign one letter to the variable one_letter
Determine the ASCII value
If ascii_letter_value =122 then (ASCII equivalent of z)
```
```
Blse if ascii_letter_value =90 then
Set new_asciii_value =65(this sets all Z's to A)
Else
Set new_ascii_value = ascii_letter_value +1(shift to next letter)
End if
Assign letters to our new string based on the ASCII values
``` Translation to Python
The function below translates our algorithm above into Python so we can start to encrypt our messages. It is designed to take in to it a string message. It will then pass through each character of the string assigning the ASCII number. It utilizes the ord() method in order to determine the ASCII numeric value. It also uses the chr() method to take our ASCII numbers and convert them to text.
```
def encryptstr(message):
#declare variables
newstring = str0
ascii_letter_value = int()
one_letter = str()
new_ascii_value = int()
#loop to assign new ASCII value
for index in range(0, len(message)):
one_letter = message[index]
ascii_letter_value = ord(one_letter)
#checking if the letter is Z or Z
if ascii_letter_value ==122:
new_ascii_value =97
elif ascii_letter_value ==90:
new_ascii_value =65
#assigning a new ascii value if not Z or Z
else:
new_ascii_value = ascii_letter_value +1
#creating the new string
newstring = newstring + chr(new_ascii_value)
return newstring
``` Finishing Up
Using this code, complete the following:
1. Write an additional function to decrypt a message
HINT: The body of this function is very similar to the encrypt function. You are simply doing the inverse of the encrypt function.
2. Build a main() function that:
a. Contains a loop that will asks the user if they want to encrypt text, decrypt text or end the program. This loop will continue to execute until the user decides to quit the program.
b. If the user wishes to encrypt or decrypt text, the program will ask them what word they want to either encrypt or decrypt.
c. If the user selects encrypt, main() will call the encrypt function and pass into it the word the user wishes to encrypt.
d. If the user selects to decrypt, main() will call the decrypt function and pass into it the word the user wishes to decrypt.
What is a Caesar Cipher? If we wanted to print

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!