Language is Python 352 Why am I getting the error if board0j

Language is Python 3.5.2

Why am I getting the error \"if board[0][j] != board[i][j]: builtins.IndexError: list index out of range\" for the code below?

import random

def start(board):
length = eval(input(\"Enter the length of a square matrix: \"))
global length
for i in range(length):
board.append([])
for j in range(length):
board[i].append(random.randint(0, 1))
print(board[i][j], end = \"\")
print()
return
  
def checkcolumns(board):
# Check columns
for j in range(length):
same = True
for i in range(1, length):
if board[0][j] != board[i][j]:
same = False
return
if same:
print(\"All \" + str(board[0][j]) + \"\'s on column \" + str(j))
isSameOnAColumn = True
return
  

def checkRows(board):
# Check rows
for i in range(length):
same = True
for j in range(1, length):
if board[i][0] != board[i][j]:
same = False
return
if same:
print(\"All \" + str(board[i][0]) + \"\'s on row \" + i)
isSameOnARow = True
return

def majorDiagonal(board):
# Check major diagonal
same = True
for i in range(1, length):
if board[0][0] != board[i][i]:
same = False
return
if same:
print(\"All \" + str(board[0][0]) + \"\'s on major diagonal\")
isSameOnADiagonal = True
return

def conditions(board):
if not isSameOnARow:
print(\"No same numbers on a row\")

if not isSameOnAColumn:
print(\"No same numbers on a column\")

if not isSameOnADiagonal:
print(\"No same numbers on the major diagonal\")

if not isSameOnASubdiagonal:
print(\"No same numbers on the sub-diagonal\")
return

def main():
board = []
isSameOnARow = False
isSameOnAColumn = False
isSameOnADiagonal = False
start(board)
checkcolumns(board)
checkRows(board)
majorDiagonal(board)
conditions(board)
  

  

main()

Solution

There were few problems with your code. In the nested for loops, j iterated from 1 to length of matrix, which will definitely throw an OutOfBounds exception. As you know, in lists, the index always starts at 0 and ends at length - 1. So when index (length) is called, it throws an OutOfBounds exception.

Further, the variables defined inside main() are not of global scope. So they cannot be accessed in other functions unless you pass them as parameters. So, either pass them as parameters or declare them outside any function that makes them global. Below is the modified correct program and the output.

import random

isSameOnARow = False
isSameOnAColumn = False
isSameOnADiagonal = False
isSameOnASubdiagonal = False

def main():
board = []
start(board)
checkcolumns(board)
checkRows(board)
majorDiagonal(board)
conditions(board)

def start(board):
length = eval(input(\"Enter the length of a square matrix: \"))
global length
for i in range(length):
board.append([])
for j in range(length):
board[i].append(random.randint(0, 1))
print(board[i][j], end = \"\")
print()
return
  
def checkcolumns(board):
# Check columns
for j in range(length):
same = True
for i in range(length):
if board[0][j] != board[i][j]:
same = False
return
if same:
print(\"All \" + str(board[0][j]) + \"\'s on column \" + str(j))
isSameOnAColumn = True
return
  
def checkRows(board):
# Check rows
for i in range(length):
same = True
for j in range(length):
if board[i][0] != board[i][j]:
same = False
return
if same:
print(\"All \" + str(board[i][0]) + \"\'s on row \" + str(i))
isSameOnARow = True
return

def majorDiagonal(board):
# Check major diagonal
same = True
for i in range(length):
if board[0][0] != board[i][i]:
same = False
return
if same:
print(\"All \" + str(board[0][0]) + \"\'s on major diagonal\")
isSameOnADiagonal = True
return

def conditions(board):
if not isSameOnARow:
print(\"No same numbers on a row\")
if not isSameOnAColumn:
print(\"No same numbers on a column\")
if not isSameOnADiagonal:
print(\"No same numbers on the major diagonal\")
if not isSameOnASubdiagonal:
print(\"No same numbers on the sub-diagonal\")
return

main()

OUTPUT

> python3 boardgame.py

boardgame.py:18: SyntaxWarning: name \'length\' is assigned to before global declaration

global length

Enter the length of a square matrix: 2

1

All 1\'s on column 0

All 1\'s on row 0

All 1\'s on major diagonal

No same numbers on a row

No same numbers on a column

No same numbers on the major diagonal

No same numbers on the sub-diagonal

Language is Python 3.5.2 Why am I getting the error \
Language is Python 3.5.2 Why am I getting the error \
Language is Python 3.5.2 Why am I getting the error \
Language is Python 3.5.2 Why am I getting the error \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site