Python Caesar密码ascii添加空格
我正在尝试制作凯撒密码,但我遇到了一个问题 它工作完美,但我想有空间添加到输入的字。如果你输入一个有空格的句子。加密时,它只打印出=而不是一个空格。有人能帮我解决这个问题,这样它就可以打印出空格了吗 这是我的密码:Python Caesar密码ascii添加空格,python,ascii,Python,Ascii,我正在尝试制作凯撒密码,但我遇到了一个问题 它工作完美,但我想有空间添加到输入的字。如果你输入一个有空格的句子。加密时,它只打印出=而不是一个空格。有人能帮我解决这个问题,这样它就可以打印出空格了吗 这是我的密码: word = input("What is the message you want to encrypt or decrypt :") def circularShift(text, shift): text = text.upper() cipher = "Cip
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')
word=input(“您要加密或解密的消息是什么:”)
def循环换档(文本,换档):
text=text.upper()
cipher=“cipher=”
对于文本中的字母:
移位=ord(字母)+移位
如果位移小于65:
移位+=26
如果移位>90:
移位-=26
密码+=chr(移位)
如果文本==(“”):
打印(“”)
返回密码
打印(word)
打印(“编码和解码的消息为:”)
打印(“”)
打印(“编码消息=”)
打印(循环移位(word,3))
打印(“解码消息=”)
打印(循环移位(word,-3))
打印(“”)
输入('按ENTER键退出')
您需要仔细检查您的状况:
给定一个空格,ord(letter)+shift
将在shift
中存储32+shift
(35当shift
为3时)。也就是说<65,因此添加26,在本例中导致61,数字为61的字符恰好是=
要解决此问题,请确保仅接触字符串.ascii_字母中的字符,例如,作为循环中的第一条语句:
import string
...
for letter in text:
if letter not in string.ascii_letters:
cipher += letter
continue
...
只需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 ("")
这里有一个我喜欢字符串。ascii字母
我不知道那里有:D