[英]Why is my code throwing an out of index error
我正在制作一个东西,它将检查一系列 arrays (实际上是一个矩阵),其中包含字母,并检查该字母是否在另一个较小的数组中,以制作大型单词搜索之类的东西
我的代码
import Board
i = 0
while i < 10:
print("whats the " + str(i + 1) + " rd ten letter line you want in the board")
temp_line = input().split()
print(temp_line)
j = 1
while j <= len(temp_line):
Board.Board[i + 1][j] = temp_line[j - 1]
j += 1
i += 1
将要清理的板子,有一个 hash 边界用于边界检查
Board = ["#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#"],
["#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#"],
["#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#"],
["#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#"],
["#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#"],
["#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#"],
["#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#"],
["#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#"],
["#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#"],
["#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#"],
["#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#"]
然后它抛出这个错误
Traceback (most recent call last):
File "main.py", line 10, in <module>
Board.Board[i + 1][j] = temp_line[j - 1]
IndexError: tuple index out of range
尽管我已经通过代码一步一步地手动查看了丑陋的板,当然看不到任何可能超出范围的地方。
它是什么?
Board.Board[i + 1][j]
返回一个IndexError: tuple index out of range
错误,因为你的板的长度是 1。
运行: len(Board.Board) //returns 1
在您的 Board 元组中添加一个左括号和右opening and closing bracket
, len(Board.Board)
现在应该返回 11 并且代码将运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.