我想通过使用echaracters
中的字符更改每个字母来加密一些字母,该字符的索引与characters
中的字母相同。
因此,我尝试使用zip()
来迭代这两个列表。
首先,我迭代了userInput
,在同一个循环中,我迭代了key
(也就是zip(characters, echaracters)
)
然后我检查i
是否等于a
,如果条件是True
,我将b
添加到cipher
userInput = input("Enter: ")
characters = ["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"]
echaracters = ["@", "$", "(", "]", "=", ")", "&", "#", "!", "%", "~", "[", "/", ";", "*", "}", "9", "?", "5", "1", "_", ">", "<<", "+", ",", "-"]
key = zip(characters, echaracters)
cipher = ""
for i in userInput:
for a, b in key:
if i == a:
cipher += b
print(cipher)
输出
Enter: ABC
@
我不明白为什么它只返回第一个符号
期望输出
Enter: ABC
@$(
我做错了什么
原因是因为'key'是一个zip类型,在第一次迭代后会丢失它的值,因为zip类型是一个迭代器Here是一个类似的问题。 你的代码有点复杂。有更简单的方法来完成你的任务
此外,如果您只需要列表而不需要字典,请不要:
你可以用
您的
key
在第一次循环传递后变为空您可以尝试以下方法:
相关问题 更多 >
编程相关推荐