Python programming please help!!!! (Python 3.5) Introduction to Problem Solving
ID: 3686873 • Letter: P
Question
Python programming please help!!!! (Python 3.5)
Introduction to Problem Solving and Programming: Cipher Program
A cipher works by replacing the characters in some input. For example, we might swap each “a” with a “b”, each “b” with a “c”, and each “c” with a “d”. That means the word “cab” would be encrypted as “dbc”:
c => d
a => b
b => c
If we know the rules of a cipher, we can also go backwards to decrypt: d => c b => a c => b So, “dbc” becomes “cab”. In this assignment, you will write a program that uses a cipher to encode and decode secret messages. Cipher Rules Use the following cipher to encode and decode your messages:
Requirements
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 next section contains sample output.
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’d get “HELLO” right back:
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
This is a limitation of the cipher we’re using. It’s not a problem that you need to account for in this assignment.
This is a sample output:
Cipher Rules Use the following cipher to encode and decode your messages: Input Replace with Input Replace with 0 % 4 5 m HfExplanation / Answer
Encryption formula:
E(x)=(x+n)mod26
Decryption:
D(x)=(x-n)mod26.
Sample code logic-
key = 'abcdefghijklmnopqrstuvwxyz'
def encrypt(n, plaintext):
"""Encrypt the string and return the ciphertext"""
result = ''
for l in plaintext.lower():
try:
i = (key.index(l) + n) % 26
result += key[i]
except ValueError:
result += l
return result.lower()
def decrypt(n, ciphertext):
"""Decrypt the string and return the plaintext"""
result = ''
for l in ciphertext:
try:
i = (key.index(l) - n) % 26
result += key[i]
except ValueError:
result += l
return result
def show_result(plaintext, n):
"""Generate a resulting cipher with elements shown"""
encrypted = encrypt(n, plaintext)
decrypted = decrypt(n, encrypted)
print 'Rotation: %s' % n
print 'Plaintext: %s' % plaintext
print 'Encrytped: %s' % encrypted
print 'Decrytped: %s' % decrypted
Sample logic for cipher-
import sys
def encrypt(k):
plaintext = raw_input('Enter plain text message: ')
cipher = ''
for each in plaintext:
c = (ord(each)+k) % 126
if c < 32:
c+=31
cipher += chr(c)
print 'Your encrypted message is: ' + cipher
def decrypt(k):
cipher = raw_input('Enter encrypted message: ')
plaintext = ''
for each in cipher:
p = (ord(each)-k) % 126
if p < 32:
p+=95
plaintext += chr(p)
print 'Your plain text message is: ' + plaintext
def main(argv):
if (len(sys.argv) != 3):
sys.exit('Usage: ceaser.py <k> <mode>')
if sys.argv[2] == 'e':
encrypt(int(sys.argv[1]))
elif sys.argv[2] == 'd':
decrypt(int(sys.argv[1]))
else:
sys.exit('Error in mode type')
if __name__ == "__main__":
main(sys.argv[1:])
note-by using the above code logics with requied modifications the given question can be answered.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.