首页 > 解决方案 > 无法对替换密码进行编码

问题描述

我正在尝试制作一个基本的密码,但我一直坚持如何让它对数据进行编码和解码。这是我到目前为止所拥有的:

alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
key =   "XPMGTDHLYONZBWEARKJUFSCIQV"
alpha = list(alpha)
print("secret decoder menu")

def encode(key, alpha):
    keyIndices = [alpha.index(k.lower()) for k in alpha]
    return ''.join(key[keyIndex] for keyIndex in keyIndices)
 
plain = input("text to be encoded: ")
print(encode(plain, key))

我不断收到此错误:

 File "/home/Deshwill/.pythonstartup.py", line 46, in <module>
    main()
  File "/home/Deshwill/.pythonstartup.py", line 32, in main
    print(encode(plain, key))
  File "/home/Deshwill/.pythonstartup.py", line 11, in encode
    keyIndices = [alpha.index(k.lower()) for k in alpha]
  File "/home/Deshwill/.pythonstartup.py", line 11, in <listcomp>
    keyIndices = [alpha.index(k.lower()) for k in alpha]
ValueError: substring not found

标签: python

解决方案


推荐阅读