所以目前,我的代码看起来像这样(感谢我在另一篇文章中提供的帮助)
phrase = raw_input("Enter text to Cipher: ")
shift = int(raw_input("Please enter shift: "))
result = ("Encrypted text is: ")
for character in phrase:
#Loops through phrase and shows ascii numbers, example: hello is: 104,101,108,108,111
x = ord(character)
#adds a shift to each character so if shift is 1 'hello' becomes: ifmmp 105,102,109,109,112
result += chr(x + shift)
print "\n",result,"\n"
问题是,如果我输入多个单词,例如: hello world ,移位为 1
输出是:ifmmp!xpsme
感叹号显示为空格(因班次而异)。我正在考虑做一个 if 语句来检测空格:
phrase = raw_input("Enter text to Cipher: ")
shift = int(raw_input("Please enter shift: "))
result = ("Encrypted text is: ")
for character in phrase:
#Loops through phrase and shows ascii numbers, example: hello is: 104,101,108,108,111
x = ord(character)
if x == ord(' '):
print "\nfound a space space"
#adds 1 to each character so 'hello' becomes: ifmmp 105,102,109,109,112
result += chr(x + shift)
print "\n",result,"\n"
但我不知道如何将空格添加到结果变量中。另外,我在这个线程中看到:Caesar's Cipher using python, can use a little help
JeffB 使用 while 循环来处理 ASCII 表 32 是空格,127 是 DEL。他为什么用96?我不明白。
while x < 32:
x += 96
while x > 127:
x -= 96
抱歉这个问题比较长。提前谢谢了!你的帮助对我来说是无价的。