首页 > 解决方案 > 代码不能采用“密钥”值,因此无法在凯撒密码上选择加密密钥

问题描述

我对python比较陌生。我写的初始版本有一个固定的密钥,abc.index(c)+3可以说也是如此。我想到了一个小升级,并决定让用户输入加密密钥。现在,如果我输入加密密钥 1-4,代码会起作用,但任何东西都不会。我现在有点卡住了。

abc = "abcdefghijklmnopqrstuwxyz"
key = int(input("what is the encryption key you desire"))
plain = input("what is the word/sentence you want to encode?: ")
cipher = "";
for c in plain:
    if c.isalpha() : cipher += abc[(abc.index(c)+key)%26]
    else: cipher += c
print(cipher)

标签: python

解决方案


将 abc 替换为

import string
abc=string.ascii_lowercase

推荐阅读