Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use python, Write a program that shall encrypt a 5-letter string using a very si

ID: 672180 • Letter: U

Question

Use python,

Write a program that shall encrypt a 5-letter string using a very simple encryption mechanism.

The program shall ask the user to enter a five-letter word consisting only of upper-case Latin letters (A through Z), and a positive integer number N. The program then shall construct another five-letter string where each character with the ASCII code X is replaced by the character with the ASCII code X+N -- that is, by the Nth next letter in the English alphabet. If X is too close to "Z", the program shall wrap around and continue at the beginning of the alphabet. For example, if X is "Y" and N=2, then X shall be replaced by an "A".

The program is allowed to fail if the user enters invalid data.

You are not allowed to use conditional statement ("if"), even if you know how to use it.

Explanation / Answer

#!/usr/bin/env python # importing the string module import string # welcome message print "Welcome to Sofian's Cesaer Encryption Algorithm!" # asking for the encryption key key = input("Please enter the encryption key (a whole number): ") # asking for the message to encrypt raw_message = raw_input("Input the message that you want to encrypt (DO NOT ENTER NUMBERS!!!): ") # converting it to lower case message = string.lower(raw_message) # intializing the variable "code" code = "" # encrypt each letter... for letter in message: if letter in string.lowercase: # if it's a character... index = string.find(string.lowercase, letter) # find it's index in the list of characters number = str(index + key) # since we start counting from zero (but normal people don't), add 1 code += "%s " % number # concatenate the letter into the final message else: if letter == " ": code += " " else: code += "%s " % letter # if it's not a character (ie. .,!?, etc...), concatenate it directly print code # print the message