列表索引运行出来了吗? Python
List index running out? Python
我正在制作一个解码器,但是当我输入一个代码字时,如果该代码字不在我在列表中输入的最后一个代码字之后,它会抛出一个错误
代码:
CodeNum = 0
while RTs < 9:
CodeWord = str(input("Code-word: "))
Stop = ["Stop", "Exit", "End", "Terminate"]
if CodeWord in Stop:
break
while CodeNum < 55:
if CodeWord == CodeWordsUpC[CodeNum]:
output.append(LettersUpC[CodeNum])
break
else:
CodeNum = CodeNum + 1
RTs = RTs + 1
错误:
Traceback (most recent call last):
File "/home/pi/mu_code/Encoder_Decoder.py", line 97, in <module>
if CodeWord == CodeWordsUpC[CodeNum]:
IndexError: list index out of range
LettersUpC = ["Q", "W", "E", "R", "T", "Y", "U", "I", "O",
"P", "A", "S", "D", "F", "G",
"H", "J", "K", "L", "Z", "X", "C", "V", "B", "N", "M", "<",
">", "_", ": ", "-"]
我忘记的是我需要将 'CodeNum' 重置为 0
output = [":", ]
RTs = 0
InOrOut = str(input("Encode or Decode, E/D: "))
if InOrOut == "D":
while RTs < 9:
CodeNum = 0
CodeWord = str(input("Code-word: "))
Stop = ["Stop", "Exit", "End", "Terminate"]
if CodeWord in Stop:
break
while CodeNum < 55:
if CodeWord == CodeWordsUpC[CodeNum]:
output.append(LettersUpC[CodeNum])
break
else:
CodeNum = CodeNum + 1
RTs = RTs + 1
print(str(output[1] + output[2] + output[3]
+ output[4] + output[5] + output[6]
+ output[7] + output[8]))
现在可以使用了,谢谢 Karl Knechtel!
我正在制作一个解码器,但是当我输入一个代码字时,如果该代码字不在我在列表中输入的最后一个代码字之后,它会抛出一个错误
代码:
CodeNum = 0
while RTs < 9:
CodeWord = str(input("Code-word: "))
Stop = ["Stop", "Exit", "End", "Terminate"]
if CodeWord in Stop:
break
while CodeNum < 55:
if CodeWord == CodeWordsUpC[CodeNum]:
output.append(LettersUpC[CodeNum])
break
else:
CodeNum = CodeNum + 1
RTs = RTs + 1
错误:
Traceback (most recent call last):
File "/home/pi/mu_code/Encoder_Decoder.py", line 97, in <module>
if CodeWord == CodeWordsUpC[CodeNum]:
IndexError: list index out of range
LettersUpC = ["Q", "W", "E", "R", "T", "Y", "U", "I", "O",
"P", "A", "S", "D", "F", "G",
"H", "J", "K", "L", "Z", "X", "C", "V", "B", "N", "M", "<",
">", "_", ": ", "-"]
我忘记的是我需要将 'CodeNum' 重置为 0
output = [":", ]
RTs = 0
InOrOut = str(input("Encode or Decode, E/D: "))
if InOrOut == "D":
while RTs < 9:
CodeNum = 0
CodeWord = str(input("Code-word: "))
Stop = ["Stop", "Exit", "End", "Terminate"]
if CodeWord in Stop:
break
while CodeNum < 55:
if CodeWord == CodeWordsUpC[CodeNum]:
output.append(LettersUpC[CodeNum])
break
else:
CodeNum = CodeNum + 1
RTs = RTs + 1
print(str(output[1] + output[2] + output[3]
+ output[4] + output[5] + output[6]
+ output[7] + output[8]))
现在可以使用了,谢谢 Karl Knechtel!