Custom Print - Print the Battleship Board - How Does it Work?


#1

I completed the assignment where we had to create a function that prints the board out, however, I do not understand how it works.

If we go back to the previous exercise called "Check it Twice", we print out the board. It has five rows of lists, each row longer by one list (for example, the first row has one list of five "O"s, the second row has two lists of five "O"s, and so on).

[['O', 'O', 'O', 'O', 'O']]
[['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]
[['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]
[['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]
[['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]

In this exercise, how does the code only print one list per row, when the previous exercise's print function printed out five rows with one list, two lists, three lists, etc.? It's iterating over each of the rows, so why doesn't it print one list for the first row, two lists for the second row, three lists for the third row, and so on?

['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']

board = []

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

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

print_board(board)

#2

Consider the .join() function when printing each row.


#3

Thank you, however, that's the step taken on the next assignment. I'm trying to understand how the function operates so as to only print one list per row, as opposed to one, two, three, etc. (depending on the row it's printing).


#4

each print statement will print on a new line by default, look:

print "hello"
print "world"

so by making a loop and using print:

for row in board:
    print (row)

each row will be printed on a new line


#5

Thanks, stetime94.

But why doesn't it print:

[['O', 'O', 'O', 'O', 'O']]
[['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]
[['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]
[['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]
[['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]

In the output above, each is a new row on a new line. I'm trying to understand why it will only print one list per row:

['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']

I suppose the issue is that if I use this code:

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

It prints the board, which, after the first iteration, is only one row of five "O"s. Then, it returns to the top of the loop, with "board" having the one row of five "O"s, and iterates again, appending another list of five "O"s to the board. So, now there are two lists of five "O"s in the second row, and so on until it loops five times, leaving five lists of five "O"s in the final row, because it kept appending a list to "board" and printing it.


#6

because each row, is one list?

this:

[['O', 'O', 'O', 'O', 'O']]
[['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]
[['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]
[['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]
[['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]

to me, looks like you used the following code:

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

this code will print the board multiply time, each time a larger version because of what you append to board, but in the end this is your board:

[['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]

If you then loop over board and print each row, you will get one list per row


#7

Okay, I think I'm starting to understand.

What is happening is that the code:

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

...is creating this:

[['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]

But when we print something every time we loop through it, it looks like:

[['O', 'O', 'O', 'O', 'O']]
[['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]
[['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]
[['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]
[['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]

However, that doesn't change the fact that, in the end, the board becomes (after looping through five times) what appears on the last line of the above, or:

[['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]

The code:

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

Iterates through that list of lists, one list at a time, and prints it on a new line.

Thank you for your help! I think I had to type it out very methodically to fully grasp what was happening here.


#8

so far so good :slight_smile:

yes, because each iteration of the loop you print the full board, which is getting bigger iteration of the loop

at the end, your whole board is indeed the following:

[['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]

You seem to grasp it now completely, the fact that you printed the full board each iteration of the loop while creating the board seem to confused you what the board was in the end, now that you understand this, you should also understand what happens when you print each row


#9

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