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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!