Row Bounds are not correct


#1



https://www.codecademy.com/courses/python-beginner-en-4XuFm/0/7?curriculum_id=4f89dab3d788890003000096#


No errors in compiling the code, but the exercise states "Oops, try again. It looks like your row bounds are not correct." I printed the row bounds and tested the random numbers the code generates, but I see no errors in boundaries. They correctly print as 4 for both rows and columns. Any assistance would be very much appreciated. Thanks.


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)
print_board(board)
# Add your code below!
def random_row(board):
    randrow = randint(0, len(board)-1)
def random_col(board):
    randcol = randint(0, len(board)-1)
random_row(board)
random_col(board)
print randint(0, len(board)-1)
print len(board)-1


#2

from the instructions:

These functions should return a random row index and a random column index

do your functions return what is asked?


#3

They do. When I printed them and ran the code several times, I do get numbers between 0 and 4.


#4

I just did further testing on the functions themselves. They return empty. However, the random numbers are generating the correct numbers.


#5

I used the return statement in the functions. Thank you for your help. Why does that work?


#6

because you deal with variable scope, look:

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)
print_board(board)
# Add your code below!
def random_row(board):
    randrow = randint(0, len(board)-1)
def random_col(board):
    randcol = randint(0, len(board)-1)
random_row(board)
random_col(board)
print randrow
print randcol

you define randrow and randcol and inside the function, so they are local variable (only exist inside the function), by returning your function can generate the random number, and you can store the result in variable when calling the functions, you will do this in a later exercise, example:

def example():
    return "Just an example"

x = example()
print x

#7

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