Download Encrypting and Decoding Secret Messages

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Encrypting and Decoding Secret Messages
Secret languages have been formed for years by people who want to send a message
to a friend but don’t want anyone else to read it. To solve this, these people make their
message hard to read for everyone but their friend. How can only their friend read it
then?
“Encrypting” means taking a message (a string) and turning it into something that
cannot be read in any another language.
“Decoding” means using a “secret code” to turn a message that can’t be read in any
other language into a message that can be easily read.
A secret code can look like the following:
H=0
i=1
!=2
So if I wrote “012” to you, you could use the code on the line above to see that my
message to you is “Hi!”
Each letter has a number assignment in Python. This is called ASCII code (pronounced
ah-see). For example, if you type in the Python command line chr(65), Python will return
the letter “A”. Likewise, if you type ord(‘A’), Python will return the number 65. You can
use the chr() and ord() commands to make your secret code.
Your mission is to make a Python program that:
1. Print a statement welcoming the user to your encrypting and deoding program
2. Asks a user for input on whether he or she would like to encrypt or decode a
message.
3. Takes the message as an input from the user.
4. Does the approrpriate task for if the user wants to encrypt or decode their
message
5. Uses a for loop to go through the message letter by letter (character by
character) and assign each letter to a number depending on your secret code.
a. My secret code could be created by adding a number to the input
argument I give to my chr() command. For example:
N = 65
chr(N) = “A”
chr(N+1) = “B”
If my message is “Hooray” and the ASCII number for H is 72, o is 111, r is
114, a is 97, and y is 121:
“Hooray” = [72, 111, 111, 114, 97, 121]
The ASCII number for I (the letter after H) is 73 (the number after 72). So:
ord(“H”) = 72;
ord(“I”) = 73;
N = 72;
chr(N) = “H”;
chr(N+1) = “I”
6. Uses a for loop to go through each number for each letter in your message and
convert it to a new letter.
My new message is: [73, 112, 112, 115, 98, 122]
Or: “Ippsbz”
7. After your code converts each letter in the message, it prints the new message
for the user.
a. If the user asked the program to encrypt a message, the program should
now print an unreadable message. If the user asked the program to
decode a message, the program should now print a readable message.
If you need any help, you can go to this website for the numbers that correspond to all
the alphabet letters and numbers according to ASCII code:
https://inventwithpython.com/chapter14.html
Answer key:
print('Welcome to Monica''s encrypting and decoding program')
e_or_d = input("Would you like to encrypt or decode?")
message = input("Enter your message here: ")
new_message = ''
if e_or_d.lower() == 'encrypt':
for letter in message:
letter_number = ord(letter);
new_letter_number = letter_number + 1;
new_letter = chr(new_letter_number);
new_message = new_message + new_letter;
elif e_or_d.lower() == 'decode':
for letter in message:
letter_number = ord(letter);
new_letter_number = letter_number - 1;
new_letter = chr(new_letter_number);
new_message = new_message + new_letter;
else:
print("Ooops! Type 'encrypt' or 'decode'")
print('Your new message is: ' + new_message)