擅长:python、mysql、java
<p>密码不能产生预期结果的原因是您的代码没有考虑它不是字母或非数字字母的情况。所以,一个潜在的解决方案就是增加对空间的处理。你知道吗</p>
<p><strong>代码</strong></p>
<pre><code>plainText = input("Secret message: ")
shift = int(input("Shift: "))
def caesar(plainText, shift):
cipherText = ""
for ch in plainText:
if ch.isalpha():
stayInAlphabet = ord(ch) + shift
if stayInAlphabet > ord('z'):
stayInAlphabet -= 26
finalLetter = chr(stayInAlphabet)
cipherText += finalLetter
elif ch is " ":
cipherText += " "
print(cipherText)
return cipherText
caesar(plainText, shift)
</code></pre>
<p><strong>示例</p>
<pre><code>Secret message: THE IDES OF MARCH
Shift: 1
UIF JEFT PG NBSDI
</code></pre>