Python Caesar encrypts ascii adds space

I'm trying to make Caesar's cipher and I have a problem with it.

It works fine, but I want to add spaces to the entered word. If you enter a sentence with spaces in it. It just prints = instead of a space when it is encrypted. Can someone help me fix this so that it prints spaces?

Here is my code:

word = input("What is the message you want to encrypt or decrypt :")
def circularShift(text, shift):
    text = text.upper()
    cipher = "Cipher = "
    for letter in text:
        shifted = ord(letter) + shift
        if shifted < 65:
            shifted += 26
        if shifted > 90:
            shifted -= 26
        cipher += chr(shifted)
        if text == (" "):
            print(" ")
    return cipher
print (word)
print ("The encoded and decoded message is:")
print ("")
print ("Encoded message  = ")
print (circularShift(word , 3))
print ("Decoded message  = ")
print (circularShift(word , -3))
print ("")
input('Press ENTER to exit')
+4
source share
2 answers

You need to carefully read your conditions:

Given a space, it ord(letter) + shiftwill store 32 + shiftin shifted(35 when shiftequal to 3). This is <65, so 26 is added, in this case it leads to 61, and the character with the number 61 appears =.

, , string.ascii_letters, , :

import string

...
for letter in text:
    if letter not in string.ascii_letters:
        cipher += letter
        continue
...
+5

split :

print (word)
print ("The encoded and decoded message is:")
print ("")
print ("Encoded message  = ")
encoded = " ".join(map(lambda x: circularShift(x, 3), word.split()))
print (encoded)
print ("Decoded message  = ")
encoded = " ".join(map(lambda x: circularShift(x, -3), encoded.split()))
print (encoded)
print ("")

+2

Source: https://habr.com/ru/post/1688925/


All Articles