编写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字符串。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值