Write a program on python using loops to decode text that was encoding using the
ID: 3670020 • Letter: W
Question
Write a program on python using loops to decode text that was encoding using the Caesar cipher.
Caesar’s cihper works by shifting every letter in a piece of text three characters over, so “A” becomes “D”, “B” becomes “E”, etc.
An example run of the program might look like:
Enter your cipher text: zhofrph wr xyd!
The decoded phrase is: welcome to uva!
An example run of the program might look like:
Enter your cipher text: fv 1110 lv pb idyrulwh fodvv!!!
The decoded phrase is: cs 1110 is my favorite class!!!
Note: To make it easier, convert all text to lowercase before doing any conversion. We will be checking for lowercase letters only. However, you should allow all punctuation and spacing to go through unchanged.
Explanation / Answer
plainText = raw_input("Could you please give me a phrase to decrypt? ")
plainText = plainText.lower()
shift = 3
cipherText = ""
for ch in plainText:
if ch.isalpha():
stayInAlphabet = ord(ch) - shift
if stayInAlphabet < ord('a'):
stayInAlphabet += 26
finalLetter = chr(stayInAlphabet)
else:
finalLetter = ch
cipherText += finalLetter
print "Your ciphertext is: ", cipherText
Hope it helps.
Please leave a comment if you have any query.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.