Write a program on python using loops to decode text that was encoding using the
ID: 3670165 • 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
# Caesar Cipher
#If for any reason raw_input() is not working then use input(). This is a problem of versions
def getMessage():
print('Enter your message:')
return raw_input()
#return input()
def getTranslatedMessage(mode, message, key):
if mode[0] == 'd':
key = -key
translated = ''
for symbol in message:
if symbol.isalpha():
num = ord(symbol)
num += key
if symbol.isupper():
if num > ord('Z'):
num -= 26
elif num < ord('A'):
num += 26
elif symbol.islower():
if num > ord('z'):
num -= 26
elif num < ord('a'):
num += 26
translated += chr(num)
else:
translated += symbol
return translated
mode = 'decrypt' #You can encript also!!!, just write mode = 'encrypt'
key = 3
message = getMessage()
print('Your translated text is:')
print(getTranslatedMessage(mode, message, key))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.