Question, is this how you do it?


#1



This code worked for me but it still displays it as a list and not just O O O O O


#Empty list
board = []

#My function to make a grid of 5x5
def battleShip(grid):
    for x in range(5):
        board.append(5 * ["O"])
    return (board)


##print (battleShip(board))

#This function displays our board using a for loop, to make each line come after each other
def displayBoard(board):
    for row in board:
        print (" ".join(row))

print (battleShip(displayBoard))


#2

here:

print (battleShip(displayBoard))

displayBoard is a function call, so it requires parentheses + argument between parentheses

first call battleShip, store the result of this function call in variable. This variable now contains the board your battleShip function returns, now you need to supply this variable as argument for displayBoard


#3

Sorry for my incapability to comprehend, how do I store the result of battleShip to a variable? Doesn't the function already do that for me?

To pass this part of the exercise was no problem. I assume because the exercise is about adding the print (" ".join(row)) to my "for loop" so that's all I had to do.

I've been playing around with the code for 30 minutes now, and still can't figure out a way to print the Os without the [] which is what I thought the exercise was about. I must be a idiot or something? :confused:


#4

given your function return the board, you will need to the result of calling battleShip in a variable, otherwise you have no variable containing your board

you must called displayBoard correctly:

displayBoard(board)

where board is your board (obviously)


#5

Thank you! Maybe I wasn't paying enough attention, I got it to print how I wanted it to now!

#Empty list
board = []

#My function to make a grid of 5x5
def battleShip(grid):
    for x in range(5):
        board.append(5 * ["O"])
    return (board)
    
board = battleShip(board)


##print (battleShip(board))

#This function displays our board using a for loop, to make each line come after each other
def displayBoard(board):
    for row in board:
        print (" ".join(row))

print displayBoard(board)

#6

quit impressive, i have seen plenty of people who struggled which the instructions i provided you, nicely done, any questions about it, or all clear>


#7

Everything is clear now ! Thank you for the response!


#8

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