我正在尝试在Python中创建一个简单的凯撒密码函数,根据用户输入移动字母并在最后创建一个新字符串。唯一的问题是最终的密文只显示最后一个移位字符,而不是包含所有移位字符的完整字符串。
以下是我的代码:
以下是我的代码:
plainText = raw_input("What is your plaintext? ")
shift = int(raw_input("What is your shift? "))
def caesar(plainText, shift):
for ch in plainText:
if ch.isalpha():
stayInAlphabet = ord(ch) + shift
if stayInAlphabet > ord('z'):
stayInAlphabet -= 26
finalLetter = chr(stayInAlphabet)
cipherText = ""
cipherText += finalLetter
print "Your ciphertext is: ", cipherText
return cipherText
caesar(plainText, shift)
cipherText = ""
之后立即 执行cipherText += finalLetter
会产生一个只包含当前finalLetter
的结果。如果人们将问题解释为“如何实现密码?”那么应该删除代码,并重新聚焦问题,提出具体要求。 - Karl Knechtel