【发布时间】:2020-05-24 06:38:05
【问题描述】:
给定以下代码:
list = [1, 0 ,3]
def decrypt(text, alphabet):
decrypt_final = ""
for j in alphabet:
aindex = alphabet.index(j)
for i in list:
if aindex == i:
decrypt_final = decrypt_final + str(j)
print(decrypt_final)
decrypt("103", "abcde")
运行代码时,结果是"abd",这不是我想要的。我正在尝试根据"abcde" 的字母范围解密数字103,如果输入为"103",正确的结果应该是"bad"。
我上面的代码试图做的是查看列表(列表中的数字来自另一个我没有包含以简化这一点的函数),如果列表编号与字母表的索引匹配,则输出字母表。不幸的是,输出顺序是错误的。
希望得到一些指导。
【问题讨论】:
-
在你的函数中你不使用参数
text。 -
你不想/不需要在这里循环
alphabet。您想遍历text中的每个字符,将其转换为数字,然后在alphabet中索引该位置...
标签: python python-3.x string encryption