python-2.7listiterator

List index out of range (Python 2.7)


I feel like I am constantly having this problem when I am writing a program, what I want to do is iterate over every value in my nested list and say that if it's not a zero, make it one.

Here is the error I am getting:

Traceback (most recent call last):
  File "C:\Users\ryry3\Desktop\Python Projects\Games\Pygame Experiment\Sprint2.py", line 65, in <module>
    resetBoard()
  File "C:\Users\ryry3\Desktop\Python Projects\Games\Pygame Experiment\Sprint2.py", line 49, in resetBoard
    if board[i][j] != 0:
IndexError: list index out of range

Here is my full code:

import random

grid = [100,100]
board = [[0,0,0,0,0,0,1,0,0,0], 
         [0,0,0,0,0,0,0,0,0,0],
         [0,0,0,0,0,0,0,0,0,0],
         [0,0,0,0,0,0,0,0,0,0],
         [0,0,0,0,0,0,0,0,0,0],
         [0,0,0,0,0,0,1,0,0,0],
         [0,0,0,0,0,0,0,0,0,0],
         [0,0,0,0,0,0,0,0,0,0],
         [0,0,0,5,0,0,0,0,0,0],
         [0,0,0,0,0,0,0,0,0,0]]


playerX = None
playerY = None
randX = 0
randY = 0

def getRandomGridPos():
    global randX, randY

    randX = int(random.uniform(0, grid[0]))     
    randY = int(random.uniform(0, grid[1]))

def main():
    pass

def printBoard():
    print str(board[0]).replace(',', '')
    print str(board[1]).replace(',', '')
    print str(board[2]).replace(',', '')
    print str(board[3]).replace(',', '')
    print str(board[4]).replace(',', '')
    print str(board[5]).replace(',', '')
    print str(board[6]).replace(',', '')
    print str(board[7]).replace(',', '')
    print str(board[8]).replace(',', '')
    print str(board[9]).replace(',', '')



def resetBoard():
    i = 0
    j = 0

    while i < 10:
        if board[i][j] != 0:
            board[i][j] = 0
            j += 1
            print "Looping"
        if j == 10:
            j = 0
            i += 1
            print "J == 10"
        if i == 10:
            j = 0
            i = 0
            print "I == 10" 
        else:
            j += 1


resetBoard()

Can someone help me find a solution and also help me not get this error anymore (explain why it happens)?


Solution

  • UPDATE:

    Sorry but I also encoutered another problem, my bad for not checking the code: The final code should be:

    while i < 10:
            if j == 10:
                j = 0
                i += 1
                print ("J == 10")
            elif board[i][j] != 0:
                board[i][j] = 0
                j += 1
                print ("Looping")
            else:
                j += 1
    

    The problem I missed was that you should have checked the j boundary before trying to access the array, only then you should start the loop again.


    The problem is in this part:

    if j == 10:
                j = 0
                i += 1
                print "J == 10"
    if i == 10:
                j = 0
                i = 0
    

    if i = 9, j= 10 then we enter into the first if which changes i,j to be 10,10
    Then the code continues and we enter the second if, thus reseting i and j making the loop go again and again...
    Solve it by deleting the second if part, since the while condition will make sure we will never go to that case in the first place.