我正在尝试创建一个caeser密码python代码,并在输入'y'时继续获取IndexError:list index超出范围。但是当我输入类似'hello'的东西时没有问题。我不一定在寻找解决方案但是我喜欢解释,因为我真的不理解它。
plaintext=raw_input("Enter message: ")
convert=plaintext.lower()
alphabet= ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
key=2
cipher=""
for i in convert:
if i in alphabet:
cipher+=alphabet[(alphabet.index(i)+key%len(alphabet))]
print("Your decrypted message is: " +cipher)
答案 0 :(得分:1)
您需要在此处移动(
)
:
cipher+=alphabet[(alphabet.index(i)+key)%len(alphabet)]
记住运营商的优先权。
您只是将%
应用于密钥