300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 编写python程序实现移位密码 凯撒密码:如何将移位作为输入?

编写python程序实现移位密码 凯撒密码:如何将移位作为输入?

时间:2022-04-28 10:42:40

相关推荐

编写python程序实现移位密码 凯撒密码:如何将移位作为输入?

这里是一个简单的版本,不需要重新排列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字符串。在

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。