简体   繁体   中英

I'm trying to make a connect four game in Python. This is my code for printing the board. Running results in a list index out of range error

Here is my code so far:

grid = [["0","1","2","3","4","5","6"],
    ['.', '.', '.', '.', '.', '.', '.'],
    ['.', '.', '.', '.', '.', '.', '.'],
    ['.', '.', '.', '.', '.', '.', '.'],
    ['.', '.', '.', '.', '.', '.', '.'],
    ['.', '.', '.', '.', '.', '.', '.'],
    ['.', '.', '.', '.', '.', '.', '.']]
itemNumber = 0
for listItem in range(len(grid)):
    line = ""
    elementNumber = 0
    for element in range(listItem):
        elementNumber = elementNumber + 1
        elementNumber = int(elementNumber)
        listItem = grid[[itemNumber][elementNumber]]
        line = line + str(listItem)
    print(line)
    itemNumber = itemNumber + 1

Running the code gives a list index out of range error on line 20. I don't understand why this is happening.

In addition, could anyone help me with finding a way to check if there is a win diagonally? I also need help with keeping the code running if someone, say, enters a column that doesn't exist.

Any help would be greatly appreciated, and a huge thanks to any contributors in advance.

I think that your problem is that you're trying to access the array like

listItem = grid[[itemNumber][elementNumber]]

instead of

listItem = grid[itemNumber][elementNumber]

and maybe you want to add the +1 to your element number "after" you have accessed the value since you will never get the first column like that

the correct way to loop through your grid would be to do this instead:

itemNumber = 0
for x in range(len(grid)):
    line = ""
    elementNumber = 0
    for element in range(len(grid[itemNumber])):
        listItem = grid[itemNumber][elementNumber]
        elementNumber = elementNumber + 1
        line = line + str(listItem)
    print(line)
    itemNumber = itemNumber + 1

Here's where I think you're doing wrong:

listItem = grid[[itemNumber][elementNumber]]

The correct way to index list of lists is list[x][y] , so in your case should be listItem = grid[itemNumber][elementNumber]

And here's another problem in your code:

while "OOOO" or "XXXX" in grid == False:
if gridSeven[column] == "X" or "O":

These are wrong formats of conditions. "OOOO" is a non-empty string which already means True, so this while loop condition is always True. Similarly, "O" means true as well. Correct format should be something like this:

while "OOOO" not in grid and "XXXX" not in grid:
if gridSeven[column] in ["X","O"]:

Let's break down your attempt to index the grid,

listItem = grid[[itemNumber][elementNumber]]

We first print the values of the variables; itemNumber and elementNumber are both 1 at this point. That makes this expression

listItem = grid[[1][1]]

To evaluate the expression grid[1][1]], we start with the index you gave, [1][1] ; note the extra barckets. Working from the inside out, ; note the extra barckets. Working from the inside out, ; note the extra barckets. Working from the inside out, [1] (the left-hand one) is a list with a single element, the integer 1 . You now try to access an element of this list, element . You now try to access an element of this list, element 1 . However, since [1] is a list with only one element, its only valid index is . However, since [1] is a list with only one element, its only valid index is 0`.

This is where your error shows up. The proper assignment is

listItem = grid[itemNumber][elementNumber]

This will evaluate grid[1][1] , which is row 2, column 2 of your 7x7 grid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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