Hide


#1

Yes, you got that right. But you want 4, because if the list of a length is 5, the indexes are: 0,1,2,3,4 and you don't want a out of range index


Hide
#2

ok but random_row or random_col never return 5,
or we want an variable with the value 1,2,3,4 and 5...
where is my error ?


#3

no, we want a variable with the value 0,1,2,3,4. I can't magically find your error, you will need to post your code


#4

( i mean my missunderstanding :wink: )

from random import randint 

board = []

for x in range(0, 5):
    board.append(["O"] * 5)

def print_board(board):
    for row in board:
        print " ".join(row)
def random_row(x):
    return randint(0,len(x)-1)
def random_col(x):
    return randint(0,len(x)-1)
    
print random_row(board)
print random_col(board)

when i call print random_row(board) and print random_col(board) the result is 0 to 4 not 1 to 5


#5

we want 0 till 4? Given lists are zero indexed based (counting start at zero)

So if the list is 5 long, the indexes are: 0,1,2,3,4. So we don't want 5, given that would be outside the list


#6

Oh ok thanks for the help


#7

we don't want 1,2,3,4,5 since then we can't access the first index (which is at zero) and 5 would be outside the list, causing errors. Confusing that counting start at zero, i know


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.