Battleship 3/19 "Make a List"


#1

I'm a bit confused here. I seem to be getting the right output on the console, (5 x 5 grid of "O"'s), but I get the following error message:

Here is the code I used:

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

Output looks like 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']
None

#2

With your code, the value of board is:

["O", "O", "O", "O", "O"]

What you did here is you printed this 5 times, but the workspace wants you to store this output in a list. Try:

board = []
# range(5) is a shortcut for range(0, 5)
for i in range(5):
    board.append(5 * ["O"]) # this appends a list of five "O" strings
for z in board:
    for y in z:
      print y
    print # blank line

Or if they want exactly what you outputted, replace the last three lines with "[tab] print z".


#3

Oh, perfect, thanks so much!

Just to make sure, when I submit this, it is accepted by the tutorial, however, the output no longer appears to be 5 x 5 grid:

O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O
O

#4

Whoops! I made that error!
In the line that says print y add a single comma after the "y". That's all.


#5

Awesome, that worked - thanks so much for your help today mathwizzy!


#6

hi...If i add single comma also, i couldnt able to get output


#7

Why does adding a single comma after the "y" change it. It chnages it from vertical list to Horizontal. I can't get why that happens.


#8

board = []

for n in range(5):
board.append(["O"] * 5)

print board

doesn't look like you'd expect, if your expecting perfect rows. But this is what the lesson asked for. Formatting is handled later.


#9

How the print statement works is that if you add a comma, it's expecting another output. Python tries to be smart by adding an extra space:

print "Hello", "World"
>>> Hello World

So if you don't put anything after the comma, Python expects another output, so it puts the next output on the same line and adds a space.


#10

how about to use

one single line code version:

board = [["O" for x in range(5)] for y in range(5)]

or

board = [[] for i in range(5)]
for i in range(5):
board[i] = ["O","O","O","O","O"]

??


#11

Everyone's problem here arises from the fact that the instructions in 3/19 already ask you to create a 5x5 grid, implying the need for rows, when in fact, it wants you to create a list.

Then, in 5/19 it wants you to turn that list into a grid! These are the instructions from 5/19:
"Now we can see the contents of our list, but clearly it would be easier to play the game if we could print the board out like a grid with each row on its own line."

This creates issues when the user gets penalised for creating a grid too early, when in fact, that is what the overall instructions of 3/19, even though the step by step instructions don't go that far.


#12

These are the codes i submitted. It worked for the question 3/19.


#13

I don't quite understand why the COMMA will do the trick, can you explain to me what did the comma did for the print? Thx :wink:


#14

In a print statement, a comma is to Python what a non-breaking space ( ) is to HTML. They both suppress line breaks.

In a row by row, col by col iteration with only one string object output per cycle, a comma would be needed to hold the print pencil in it's current position on the line. The presents with a different problem, how to break up the single line that will result.

Eg.

for row in rows:
    print
    for col in row:
        print "O ",

The code might not be right, but you get the idea. Start each outer loop with a print to reset the pencil (as I call it).


#15

Thanks @mtf ! with your explanation now I understand how it works. :smile:


#16

I don't understand why all the above answers are leaving out the function
def print_board(board): that the question asks for. Does another for loop replace the function?


#17

the lesson is a bit silly there!


#18

The OP marked it solved in post #4. After that it's just been discussion.


#27

that's very helpful thankyou :smiley:


#28

your solution is much more elegant than I thought to use:

count = 0
while (count < 5):
    print ["O"] * 5
    count = count + 1