简体   繁体   English

为什么我的代码会抛出索引错误

[英]Why is my code throwing an out of index error

I'm making a thing that will check through a series of arrays (effectively a matrix) with letters in them and check if that letter is within another smaller array to make a mega wordsearch sort of thing我正在制作一个东西,它将检查一系列 arrays (实际上是一个矩阵),其中包含字母,并检查该字母是否在另一个较小的数组中,以制作大型单词搜索之类的东西

My code我的代码

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

The board that will be cleaned up, has a hash border for border checking将要清理的板子,有一个 hash 边界用于边界检查

Board = ["#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#"],
["#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#"],
["#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#"],
["#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#"],
["#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#"],
["#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#"],
["#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#"],
["#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#"],
["#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#"],
["#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#"],
["#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#"]

And then it throws this error out然后它抛出这个错误

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

Despite the ugly board I've looked manually step by step through the code and of course do not see where anything could possibly be out of range.尽管我已经通过代码一步一步地手动查看了丑陋的板,当然看不到任何可能超出范围的地方。

What is it?它是什么?

Board.Board[i + 1][j] returns an IndexError: tuple index out of range error because the length of your board is 1. Board.Board[i + 1][j]返回一个IndexError: tuple index out of range错误,因为你的板的长度是 1。

Running: len(Board.Board) //returns 1运行: len(Board.Board) //returns 1

Add an opening and closing bracket to your Board tuple and len(Board.Board) should now return 11 and the code will run.在您的 Board 元组中添加一个左括号和右opening and closing bracketlen(Board.Board)现在应该返回 11 并且代码将运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM