7/19


#1

I can't figure out what I am doing wrong, the error I am getting quotes line 5 which I didn't even write, code academy did and it looks fine to me and matches what others have posted. Lend a girl a hand?

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)

# Add your code below!
def random_row(board):
    return randint(0, len(board) - 1)
    
def random_col(board):
    return randint(0, len(board) - 1)
    
print random_row(board)
print random_col(board)

the error I'm getting is

Traceback (most recent call last):
  File "python", line 5, in <module>
TypeError: 'list' object is not callable

#2

I was having nearly the same problem and I found the solution to be just cutting the code that we are given. I used:

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(board):
    return randint(0, 4)
def random_col(board):
    return randint(0, 4)
random_row(board)
random_col(board)

#3

@cjacobmtz

Your code worked but I'm unsure for the reason we have to change the code that Codecademy has provided, shouldn't it work as we are expecting? This is the most frustrating thing about learning any kind of code, examples not responding as indicated!

Thanks for the code snippet.


#4

@cjacobmtz your code didnt work for me either.

I'm having the worst time with this module, what does Call each function on board. refer to?


#5

I agree that it is frustrating but I can see the value in learning more ways to do the same thing. You aren't always going to be working with static lists and randint(0, len(board) - 1) will always work even if you end up changing the amount of items in the list. Going back, I was able to make it work by copying and pasting the snippet above. The code as Codecademy wants it to be is:

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(board):
    return randint(0, len(board) - 1)
def random_col(board):
    return randint(0, len(board) - 1)
random_row(board)
random_col(board)

#6

"Call each function on board" refers to actually running the functions after you create them. This is what generates the ship's location. It is the last part of the code:

random_row(board)
random_col(board)