我有这个代码,这是一个基本的凯撒密码,偏移量设置为1,但我想要它,以便用户可以输入偏移量。用户应该能够说出字母表的移动量,但是如果offset=input,它就不起作用了
#Caesar cipher
sentance = input('Enter sentance: ')
alphabet = ('abcdefghijklmnopqrstuvwxyz')
offset = 1
cipher = ''
for c in sentance:
if c in alphabet:
cipher += alphabet[(alphabet.index(c)+offset)%(len(alphabet))]
print('Your encrypted message is: ' + cipher)
这可能是因为您没有将输入转换为整数,实际上是用字符串填充
offset
。在使用
int(input())
将输入的字符串转换为整数(可选-还记得添加原始字符,以防它们不在字母表中):将产生:
^{pr2}$相关问题 更多 >
编程相关推荐