这里是一个简单的版本,不需要重新排列alpha字符串。注意,这并不能解释用户输入的错误,比如输入一个单词而不是一个数字来进行旋转。在while 1:
rot = int(raw_input("Enter Rotation: "))
cipher = raw_input("Enter String: ")
secret,alpha = '', 'abcdefghijklmnopqrstuvwxyz'
for i in cipher.lower(): #Loop through the original string
if i not in alpha: #If the character in the original string is not in the alphabet just add it to the secret word
secret += i
else:
x = alpha.index(i) #Get index of letter in alphabet
x = (x+rot)%26 #Find the index of the rotated letter
secret += alpha[x] #Add the new letter to the secret word
print f
你可以把for循环中的所有内容压缩成一行,但这样读起来就不那么好看了
^{pr2}$
如果你想对你的凯撒密码感兴趣,那就找一个有钥匙的凯撒密码,并在里面加上这个选项。但这需要操作alpha字符串。在