def caesar(plaintext,shift):
alphabet=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
#Create our substitution dictionary
dic={}
for i in range(0,len(alphabet)):
dic[alphabet[i]]=alphabet[(i+shift)%len(alphabet)]
#Convert each letter of plaintext to the corrsponding
#encrypted letter in our dictionary creating the cryptext
ciphertext=("")
for l in plaintext.lower():
if l in dic:
l=dic[l]
ciphertext+=l
return ciphertext
#Example useage
plaintext="the cat sat on the mat"
print "Plaintext:", plaintext
print "Cipertext:", (caesar(plaintext,29))
cipertext只打印一个字母,而不是在caesar shift中打印'明文'变量。我希望它能打印整个句子。Python - 为什么这个循环只打印1个字母?
感谢
感谢我现在的工作。但还有一个问题。凯撒没有空格,它只是一串字母,就像这样:wkhdwvdwrqwkhpdw – BubbleMonster
这是因为它不符合'if if in dic'的标准,所以你需要为这样的情况制作一个'else'声明,无论如何。 (我不认为你在轮班中改变了标点符号)。 –