Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 将1个列表中的1个字符分配给另一个python_List_Python 2.7 - Fatal编程技术网

List 将1个列表中的1个字符分配给另一个python

List 将1个列表中的1个字符分配给另一个python,list,python-2.7,List,Python 2.7,嗨,我正在为我的学校项目制作一台解密机,但我无法让它工作,你们能帮我吗? 已经谢谢你了 错误是:第17行,索引器:列表索引超出范围 zin=86的长度,正如你所知 这就是我需要解密的文件:KEIGO N Jidoubaneuofideesun IRAEI Estigivnkmueer RDONAEOIW ENEZAEE NAML VN NILLRA with open('something.txt', 'r') as fhandle: key = 3 #reading the file zi

嗨,我正在为我的学校项目制作一台解密机,但我无法让它工作,你们能帮我吗? 已经谢谢你了

错误是:第17行,索引器:列表索引超出范围 zin=86的长度,正如你所知

这就是我需要解密的文件:KEIGO N Jidoubaneuofideesun IRAEI Estigivnkmueer RDONAEOIW ENEZAEE NAML VN NILLRA

with open('something.txt', 'r') as fhandle:

key = 3

#reading the file
zin = list(fhandle.readline())

#setting up solution to which we will output
solution = list(" ")*86
solution[0] = zin[0]

#while loop in which we use the key to decrypt the message
i = 1
while i < len(zin):
    solution[i] = zin[key] #this is where i get the error
    i += 1
    key += key
    if i > 86:
        break

print(solution)
以open('something.txt',r')作为句柄的
:
键=3
#读取文件
zin=list(fhandle.readline())
#设置我们将输出到的解决方案
解决方案=列表(“”*86
溶液[0]=锌[0]
#while循环,在该循环中,我们使用密钥解密消息
i=1
而我86:
打破
打印(解决方案)

由于您正在访问
zin[key]
,您需要验证
zin
的长度至少为
key+1

您需要将错误添加到此问题中。看起来在某个点上,zin的长度小于3,因此你得到了一个索引器,但这只是一个猜测。那么最好的办法是在
solution[i]=zin[key]
行上方放置一个print语句,该行的内容为
print len(zin)
。这将告诉您,您从带有
zin=list(fhandle.readline())
的文件中读取的内容可能不是您所期望的。请改用fhandle.readlines(),它将提供完整的行列表。fhandle.readline()只给出文件的第一行。为什么要硬编码
86
?如果
zin
的长度不同怎么办?另外,除非我弄错了,否则在循环的每次迭代中,您都要加倍
key
,即在
I
迭代之后,
key
key*2^I
,它明显大于
zin
的长度。你的意思是
zin[key%len(zin)]
?每次重新循环后,我都会运行它并打印出来。我注意到在某一点上,键变得大于86,这是句子的长度。所以现在我需要知道如何使密钥计数超过86并从0开始