Ascii Code
Ascii Code
Write a program to encode and decode a message. Use the following guidelines to write your
program:
1. Think of a secret message you want to encode. Be creative! Maybe a famous quote, a favorite
lyric, or your personal motto.
2. Using the ord() and chr() functions, encode and decode the message.
3. Optional: Ask the user to guess the message before revealing it.
Display the encoded message in binary, or include a twist by adding 2 to the number.
When decoding, remember to reverse the steps.
4. Neatly print the encoded and decoded messages to the screen.
5. Write the pseudocode for this program. Be sure to include any needed input, calculations, and
output.
Insert your pseudocode here:
# 2. Define a function encode_message(message) to encode the message using only chr() and
ord()
- Initialize an empty string encoded_message
- For each character in the message:
- Convert the character to its ASCII value using ord()
- Append the ASCII value followed by a space to encoded_message
- Return the stripped encoded_message
Example of expected output: The output for your program should resemble the following
screen shot. Your specific results will vary depending on the choices you make and the input
provided.
Insert a copy of your code from IDLE here:
# 1. Secret message
secret_message = "I love python!"
def decode_message(encoded_message):
decoded_chars = [chr(int(char)) for char in encoded_message.split()]
decoded_message = ''.join(decoded_chars)
return decoded_message
print("\nEncoded Message:")
print(encoded_message)
if user_guess.lower() == secret_message.lower():
print("Congratulations! Your guess is correct.")
else:
print("Incorrect guess. The secret message is:", secret_message)
print("\nDecoded Message:")
print(decoded_message)
How could your program be useful in the real It can help teach people about ASCII code and how
world? it works, as well as entertain people with a fun
game.
What is a problem you ran into, and how did you I kept mixing up the ord and char functions, so I
fix it? would have to re-order my code. Once I got a hang
of it, I wasnt making the same mistake again.
Describe one thing you would do differently the I would add more than one password and add hints
next time you write a program. to make the game easier.