Using Pyton 3 write a cipher program. In this programming assignment, you will w
ID: 3597150 • Letter: U
Question
Using Pyton 3 write a cipher program.
In this programming assignment, you will write a program called cipher.py that uses a cipher to encode and decode secret messages.
Cipher Rules:
Use the following cipher to encode and decode your messages:
Explanation / Answer
cipher_dict = {
'a': '0',
'b': '1',
'c': '2',
'd': '3',
'e': '4',
'f': '5',
'g': '6',
'h': '7',
'i': '8',
'j': '9',
'k': '!',
'l': '@',
'm': '#',
'n': '$',
'o': '%',
'p': '^',
'q': '&',
'r': '*',
's': '(',
't': ')',
'u': '-',
'v': '+',
'w': '<',
'x': '>',
'y': '?',
'z': '='
}
def getDecoderFromEncoder(encoder_dict):
dec_dict = {}
for key in encoder_dict:
dec_dict[encoder_dict[key]] = key
return dec_dict
decoder_dict = getDecoderFromEncoder(cipher_dict)
def encoder(plaintext):
cipher = ""
for c in plaintext:
if c in cipher_dict:
cipher += cipher_dict[c]
else:
cipher += c
return cipher
def decoder(cipher):
plaintext = ""
for c in cipher:
if c in decoder_dict:
plaintext += decoder_dict[c]
else:
plaintext += c
return plaintext
while True:
print("Welcome to the Secret Message Encoder/Decoder")
print("1. Encode a message")
print("2. Decode a message")
print("3. exit")
print()
choice = int(input("What would you like to do? "))
if choice == 1:
plainText = input("Enter a message to encode: ")
plainText = plainText.rstrip()
print("Encoded message:", encoder(plainText))
print()
elif choice == 2:
cipher = input("Enter a message to decode: ")
print("Encoded message:", decoder(cipher))
print()
elif choice == 3:
break
else:
print(choice, "is not a valid choice")
# copy pastable code link: https://paste.ee/p/ITSUx
'''
Samle run
Welcome to the Secret Message Encoder/Decoder
1. Encode a message
2. Decode a message
3. exit
What would you like to do? 1
Enter a message to encode: hello world
Encoded message: 74@@% <%*@3
Welcome to the Secret Message Encoder/Decoder
1. Encode a message
2. Decode a message
3. exit
What would you like to do? 2
Enter a message to decode: 74@@% <%*@3
Encoded message: hello world
Welcome to the Secret Message Encoder/Decoder
1. Encode a message
2. Decode a message
3. exit
What would you like to do? 3
'''
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.