一个学校项目的程序设计基于两个列表的索引对用户输入进行加密/解密。我的问题是为列表编制索引,并将用户输入与索引进行比较,以便屏幕的输出=list2
所有的代码都非常基本,在变量名中使用'str'和'input',以免混淆我自己。你知道吗
list1 = ['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','.','1','2','3','4','5','6','7','8','9','0']
list2 = ['4','R','5','G','Z','3','2','D','A','E','X','Y','U','I','6','W','7','O','V','8','F','Q','0','L','J','.','H','9','C','B','N','S','P','M','1','T','K']
strInput = input("Type the message you would like to Encrypt ").upper()
inputList = split(strInput)
print(inputList)
i = 0
for char in inputList:
if inputList[i] != list1[i]:
i = i + 1
现在,它应该接受用户输入,通过索引比较每个列表上的位置,然后使用另一个列表将文本打印到屏幕上。我就是想不出我的索引问题。 提前感谢您的帮助!你知道吗
您可以使用
.index(char)
在第一个列表中找到字符的索引您还可以使用python的列表理解docs
要解密你基本上会做相反的事情。你知道吗
答:我不知道你的代码要去哪里,也不知道作业是什么,但我想我是为你做的。你知道吗
别养成让别人帮你做作业的习惯,你不会学到东西的
例如字符串“youwill fail the exam”给出
['J', '6', 'F', '0', 'A', 'Y', 'Y', '3', '4', 'A', 'Y', '8', 'D', 'Z', 'Z', 'L', '4', 'U']
其他答案也适用,不过您可能需要考虑使用
dict
来提高速度。python字典是一个键值对表(hash表是技术性的)。表使用键查找值。当您在dict
中搜索某个元素时,is有一个恒定的查找时间O(1),这意味着dict
不会通过自身搜索该元素。它确切地知道它在哪里(如果它有)。你知道吗例如:
您的列表:
变成
dict
:然后我们可以加密:
测试:
输入:
Test 2
输出:
8ZV8 C
相关问题 更多 >
编程相关推荐