[英]List Index Out Of Range Error on python 3.x
error message: 错误信息:
Traceback (most recent call last): File "C:\\Users\\Name\\Desktop\\transpositioncipher.py", line 17, in cipher.append(stringList[(x*result)+i]) 追溯(最近一次通话最近):文件“ C:\\ Users \\ Name \\ Desktop \\ transpositioncipher.py”,位于cipher.append(stringList [(x * result)+ i])中的第17行
IndexError: list index out of range IndexError:列表索引超出范围
I am making a Transposition Cipher to encrypt a string. 我正在制作转置密码来加密字符串。 I will be working on decryption later. 稍后我将进行解密。
I have been running into this error with no luck of fixing it. 我一直遇到这个错误,没有运气修复它。 The output should be [f, u, o, r] if the input text is 'four'. 如果输入文本为“四个”,则输出应为[f,u,o,r]。
import math
stringToEnc = input("String to encrypt: ")
stringList = list(stringToEnc.replace(" ", ""))
letterCount = len(stringList)
encrypted = []
result = letterCount
for i in range(2, int(math.sqrt(letterCount))+1):
if letterCount % i == 0:
result = i
break
i = 0
while i != letterCount:
for x in range(result):
encrypted.append(stringList[(x*result)+i])
i+=1
print(encrypted)
If you look in the debugger, or add print ((x*result) + i)
into the while loop, you will see that print ((x*result) + i)
returns 4 on the 3rd iteration of the for loop. 如果您在调试器中查找,或者将print ((x*result) + i)
到while循环中,您将看到print ((x*result) + i)
在for循环的第3次迭代中返回4。 4 is out of bounds for stringList
which has a length of 4, but only the indexes 0,1,2,3. 长度为4的stringList
超出限制4,但只有索引0、1、2、3。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.