Could anyone please help with this Python code? In this programming assignment,
ID: 3595132 • Letter: C
Question
Could anyone please help with this Python code?
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:
Input
Replace with
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
=
The program should start with a menu. It should ask the user whether they/d like to encode a message, decode a message, or exit. The following section contains sample output.
Sample Output
(Input is highlighted in orange)
Limitations
Note that the cipher rules described above do not account for uppercase characters, numbers or symbols in the user’s input. In other words, if you were to encode the word “HELLO”, you would get “HELLO” right back:
Example:
This is a limitation of the cipher we are using. This is not a problem that you need to account for in this assignment.
Testing
Once you have written your program, you need to test it. You should run your program and enter a variety of messages to encode and decode. Make sure to handle any errors in the menu-selection as well.
Example:
Input
Replace with
a
0
b
1
c
2
d
3
e
4
f
5
g
6
h
7
i
8
Explanation / Answer
# This program has been written for Python 2.7
#
# Output from the below program:
#
# 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
# Decoded 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? 1
#
# Enter a message to encode: this message is top secret
# Encoded message: )78( #4((064 8( )%^ (42*4)
# 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: )78( #4((064 8( )%^ (42*4)
# Decoded message: this message is top secret
# 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
# Encoded message: HELLO
# Welcome to the Secret Message Encoder/Decoder
# 1. Encode a message
# 2. Decode a message
# 3. Exit
#
# What would you like to do? WORLD
#
# Please enter a number between 1 and 3.
# Welcome to the Secret Message Encoder/Decoder
# 1. Encode a message
# 2. Decode a message
# 3. Exit
#
# What would you like to do? one
#
# Please enter a number between 1 and 3.
# Welcome to the Secret Message Encoder/Decoder
# 1. Encode a message
# 2. Decode a message
# 3. Exit
#
# What would you like to do? 0
#
# 0 is not a valid choice
# Welcome to the Secret Message Encoder/Decoder
# 1. Encode a message
# 2. Decode a message
# 3. Exit
#
# What would you like to do? 4
#
# 4 is not a valid choice
# Welcome to the Secret Message Encoder/Decoder
# 1. Encode a message
# 2. Decode a message
# 3. Exit
#
# What would you like to do? 3
def menu_driven():
cipher_map={'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':'='}
while (True):
print "Welcome to the Secret Message Encoder/Decoder";
print "1. Encode a message"
print "2. Decode a message"
print "3. Exit "
choice = raw_input("What would you like to do? ")
print ""
if choice.isdigit():
choice = int(choice)
if choice <= 3 and choice >= 1:
if choice == 1:
message = raw_input("Enter a message to encode: ")
output = encoded_message(cipher_map, message)
print "Encoded message:",output
elif choice == 2:
message = raw_input("Enter a message to decode: ")
output = decoded_message(cipher_map, message)
print "Decoded message:",output
elif choice == 3:
break;
else:
print choice,"is not a valid choice"
else:
print "Please enter a number between 1 and 3."
def encoded_message(cipher_map, message):
output = ""
for character in message:
if character.islower():
output = output + cipher_map[character]
else:
output = output + character
return output
def decoded_message(cipher_map, message):
output = ""
reverse_cipher_map = {value: key for key, value in cipher_map.iteritems()}
for character in message:
if character in reverse_cipher_map:
output = output + reverse_cipher_map[character]
else:
output = output + character
return output
menu_driven()