Battleship - Custom Print


#1

I am working on the mentioned section and found a YouTube video to show me the answer, but there is not a lot of technical explanation. My code is the following and it was accepted for this lesson:

board = []

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

def print_board(board):
    for i in board:
        print i
print_board(board)

Why do I have to use "print i", what am I telling python to do with that command? I apologize if this is a basic question, but I want to know how the code would sound, in my head, if I was speaking the directions.

Thanks


#2

board is a multi-dimensional list, by looping over it, you get access to the inner lists, which you print (i)


#3

Stetim94, the concept of inner lists is not clear to me. Can you further explain inner lists? I am looking at the code as for i in board and how does that code dictate the format of the output?

I appreciate your assistance.


#4

well this a normal list:

[1,2,3]

its 1 dimensional. but we can also nest a list inside a list:

[[1,2,3],[4,5,6],[7,8,9]]

which is 2 dimensional. Just like your board. an outer list (in case of your board, that are the columns) and inner lists (your rows)


#5

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