Question
How do I keep the spaces in between words in a phrase when using caesar cipher in python. The message I want to encode and
How do I keep the spaces in between words in a phrase when using caesar cipher in python. The message I want to encode and decode is "the ships sail at midnight" with a shift key value of "4".
This is my code:
def main(): print("Caesar Cipher") print()
key = int(input("Enter the key value: ")) plaintext = input("Enter the phrase to encode: ") validLetters = "abcdefghijklmnopqurstuvwxyz " cipher = "" decrypt = "" space = " " for letter in plaintext: cipher = cipher +chr((ord(letter.lower())+key - ord("a"))%26+ord("a")) for letter in cipher: decrypt = decrypt + chr((ord(letter.lower())-key - ord("a"))%25+ord("a")) cipher = cipher.upper() decrypt = decrypt.upper()
print("Original message was: ",plaintext) print() print("Encoded message follows: ",cipher) print() print("Decrypted message follows: ",decrypt) print()
main()
and this is my result:
Caesar Cipher
Enter the key value: 4 Enter the phrase to encode: the ships sail at midnight Original message was: the ships sail at midnight
Encoded message follows: XLIRWLMTWRWEMPREXRQMHRMKLX
Decrypted message follows: THENSHIPSNSAILNATNMIDNIGHT
How do I maintain those spaces?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started