# Battleship! Make a List

#1

I feel this code is pretty clean and close. If only I could get this to print 5 times for the rows. Will this work if I can get it to print 5 rows somehow. So far if I print *5 somewhere it just prints one long vertical line of O’s…

Currently it prints: [‘O’, ‘O’, ‘O’, ‘O’, ‘O’]

for the assignment I get error:" It looks like you do not have 5 rows in your list." So pretty close?!

``````
def createboard(spaces):
board = []
for space in range(len(spaces)):
board += spc * 5
return board

spc = ["O"]

createboard(spc)

``````

#2

for complete in board:
print [complete] * 5

so complete code is:

``````
def createboard(spaces):
board = []
for space in range(len(spaces)):
board += spc * 5
for complete in board:
print [complete] * 5

spc = ["O"]

createboard(spc)

``````

and return board and it looks like i get a good result but still error: It looks like you do not have 5 rows in your list.

But it looks like 5 rows of 5 lists…

[‘O’, ‘O’, ‘O’, ‘O’, ‘O’]
[‘O’, ‘O’, ‘O’, ‘O’, ‘O’]
[‘O’, ‘O’, ‘O’, ‘O’, ‘O’]
[‘O’, ‘O’, ‘O’, ‘O’, ‘O’]
[‘O’, ‘O’, ‘O’, ‘O’, ‘O’]

#3

``````for row in board:
print ' '.join(row)
``````

What does it print?

#4

Sorry, where do I put it. Not under the “createboard(spc)” at the very end?

when I print

``````
def createboard(spaces):
board = []
for space in range(5):
board.append("O")
for complete in board:
print [complete] * 5

spc = ["O"]

createboard(spc)
for row in board:
print ' '.join(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’]
O O O O O
O O O O O
O O O O O
O O O O O
O O O O O

^ that gets printed, I am doing this in my Jupyter Python notebook though. In codeacademy everything prints the same with same error saying it looks like I don’t have 5 rows even though it prints the 5 lists.

#5

Okay, that tells us the board is visible in the global namespace.

We’ll run that code again, this time as a function. (Be sure to write the function above its first call.)

``````def print_board():
for row in board:
print ' '.join(row)
``````

Now let’s try another debugging test…

``````board[0][0] = 'X'
print_board()
``````

What does it print?

#6
``````

def createboard(spaces):
board = []
for space in range(5):
board.append("O")
for complete in board:
print [complete] * 5

spc = ["O"]

def print_board():
for row in board:
print ' '.join(row)
createboard(spc)

``````

in a new jupyter notebook it prints just:

[‘O’, ‘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 next cell I enter:

board[0][0] = 'X’
print_board()

I get:

NameError Traceback (most recent call last)
in ()
----> 1 board[0][0] = 'X’
2 print_board()

NameError: name ‘board’ is not defined

in CodeAcademy after printing everything together I get:

[‘O’, ‘O’, ‘O’, ‘O’, ‘O’]
[‘O’, ‘O’, ‘O’, ‘O’, ‘O’]
[‘O’, ‘O’, ‘O’, ‘O’, ‘O’]
[‘O’, ‘O’, ‘O’, ‘O’, ‘O’]
[‘O’, ‘O’, ‘O’, ‘O’, ‘O’]
Traceback (most recent call last):
File “python”, line 16, in
IndexError: list index out of range

#7

in another jupyer notebook instance I got a different error after the debug part in a different cell:

IndexError Traceback (most recent call last)
in ()
----> 1 board[0][0] = 'X’
2 print_board()

IndexError: list index out of range

#8

Going back to my original test, which centered upon scope. The board list has been defined inside a function. It is not a global object in that context. That is the issue to solve, first. then continue with the debug testing. Our second test is still important to explore, but let’s deal with this scope issue first.

#10

Ah, after moving on I realized you were helping me with steps far beyond 3. The instruction in CodeAcademy are kind of misleading at times I think. Like in “Custom Print” lesson 5 instructions say: Then, define a function named print_board with a single argument, board_in.

But in reality you need ‘board’ not board_in for the argument… I am confused.

#11

The board is a global object, but the instructions want us to treat it as a parameter in any function that accesses it.

``````def print_board(board_in):
``````

and the call,

``````print_board(board)
``````

Inside the function use the parameter, board_in. You have the code already.

As for creating your board, you have that code, too, but need to assign the return value to a variable…

``````board = createboard(...)
``````

I just realized that the course has been rewritten and has a whole different set of instructions than we;ve been used to these past several years. Will need to check the instructions to get up to date.

Seems they might not have changed as much as I thought. The first step (lesson 2) is to create an empty list called board. There is no createboard function.

The best way to make it through these courses is to not deviate from the instructions. Follow them to the letter for best results and wait until you reach the end of the module to explore the many other options still open. Don’t use the lesson as a playground, or you’ll never make it through the course and cause a real ruckus of confusion in the Q&A forums.

Go back to step 2 and start over.

``board = []``

#12

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