我正在尝试编写一个程序,该程序以随机顺序生成一个从 1 到 26 的数字列表,然后使用该列表“加密”给定的单词,以便将字母表的第 n 个字母映射到第 n 个随机列表中的数字。例子:
随机列表是:
[8,2,25,17,6,9,12,19,21,20,18,3,15,1,11,0,23,14,4,7,24,5,10,13,16,22]
这意味着单词act
变成[8,25,7]
,单词xyzzy
变成[13,16,22,22,16]
。
我有以下代码,但我不确定如何继续:
#8a
def randomalpha():
a=[0]*26
count = 0
while count < 25:
r = randrange(0,26)
if r not in a:
a[count] = r
count += 1
return(a)
print(f())
#8b
ls=['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']
def encrypt(alphabet):
a=randomalpha()
count=0
b=input('enter a word')
for i in b: #not sure if i am ok up to here but this is when i got really confused
print(encrypt(ls))