3/19 - Function is passing but not sure if correct


#1

I'm on step 3 (Make a list) of Battleship! and I have code that is getting the green light but I am not sure if it is correct and would like to understand better.

My code is below. I'm getting 5 rows of O's, however I don't believe it's storing the output into my emtpy "board" list.


board = []
for i in range(0,5):
   i = ['O']*5
   
   print i

Any suggestions or further explanation would be greatly appreciated.

Thank you


#2

you are not storing the rows in your board. .append() i(which contains your row) toboard list


#3

Hi, thanks for answering so quickly. When I add board.append(i) and print board inside the loop, my output 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']]

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

#4

yes, that is good :slight_smile:

Or do you have any questions about it?

because you put print board inside the loop, you see the board being build, one row being added a time, in the end, your board has 5 rows


#5

Ah, now I understand. I'm seeing each iteration of the loop be put inside my board list. I was confused on how the output should look. I thought it was supposed to display simply the last line, with 5 rows of O's.


#6

well, you can achieve that by simply placing print board outside the loop, then the board will be printed only after be constructed

i think you understand, you can progress to the next exercise, where you will make a function to print the board pretty so you can present it nicely to the user


#7

Yes, I understand now. Thank you so much for your time and help! :+1:


#8