FAQ: Learn Python - Battleship - Make a List

This community-built FAQ covers the “Make a List” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise Make a List:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!

Other FAQs

The following are links to additional questions that our community has asked about this exercise:

  • This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
  • Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
  • This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!

Not seeing your question? It may still have been asked before – try (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

Exercise suggests to do the following:

board=

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

But doesn’t accept :

board=[[“O”]*5]*5

Although it looks like the same result to me, what’s wrong?

This will not give independent rows. They are all references to the same five cells.

Try this (be sure you have a print_board function)…

board=[[“O”]*5]*5
board[2][2] = "X"
print_board(board)

Do you see a column of X’s?

I do, thank you!
I can’t still quite understand why would you need two indexes?
I’ve tried to mess around, seems like the first one can be changed, and the second index refers to the column.

board [2][2]=“X” -> changes all in the second column
board[1][2]= “X” -> changes all in the second column
board[2]= “X” -> the middle row changes to only “X” instead of having all the “O”

The first subscript is the index of the row, the second subscript is the index of the column.

Hello I Have a different question. I got an error: It looks like the rows are not represented as lists.
I have to admit my code was rather wild, but the result looks exactly as the solution. It goes like this:
line =
board =
def create_line(x):
for position in range(5):
x.append(“O”)
return x

def create_board(y):
result = [create_line(board)] * 5
return result
print create_board(board)

A little side experiment to try on your code.

Create the board using your code. Then set an element and print the board.

board[2][2] = "X"

What does the board look like when you print it? A screenshot will do.

Oh sorry for not replying I didn’t get to coding last 2 weeks now I started the exercise from scratch and I made it working. Thank you very much for your effort.

1 Like

i do not understand the range function’s effect in this exercise.
lines of accepted codes are as follows:
board=

for stuff in range(0,5):

board.append([“O”]*5)

print board

when the board is printed, it does not give 5 equal rows of [‘o’,‘o’,‘o’,‘o’,‘o’,] but rather it gives cascading rows as such:
[‘o’,‘o’,‘o’,‘o’,‘o’,]
[‘o’,‘o’,‘o’,‘o’,‘o’,][‘o’,‘o’,‘o’,‘o’,‘o’,]
[‘o’,‘o’,‘o’,‘o’,‘o’,][‘o’,‘o’,‘o’,‘o’,‘o’,][‘o’,‘o’,‘o’,‘o’,‘o’,]
[‘o’,‘o’,‘o’,‘o’,‘o’,][‘o’,‘o’,‘o’,‘o’,‘o’,][‘o’,‘o’,‘o’,‘o’,‘o’,][‘o’,‘o’,‘o’,‘o’,‘o’,]
[‘o’,‘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 thought the purpose of this was to have a result more 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’,]

I can’t but guess your indentation. When you post code, use the </> icon at the right of the menu bar atop the text box you are using.

Is it like this?

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

If so, it gives the expected output, a list of lists of the character ‘O’:

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

… which when printed with the appropriate formatting, gives what you were looking for.

If, however, it is indented like this:

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

… you get 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']]

… the last line of which is what you want. The indentations, in Python, do count. … as do commas (compare with what you’ve shown.)

1 Like

The range function has one purpose, to create an iterable in Python 2 and an iterator in Python 3. It doesn’t matter the difference if we use for to handle the iteration. It still works as it always did.

for x in range(10):

will still give x all the values between 0 and 9 inclusive, in turn.

The old range returned a list which took up space in memory for every element in the list. The new range doesn’t take up any more space for one or a thousand possible elements. Only thing is to get to 1000, it must iterate through the other 0-999. That’s code doing that, though, not something that will ever take up more space in memory than it does. At the end of the day we still have a sequence. An iterator is a potential sequence with a method that knows where the next item is in the sequence given the starting and ending point, and the stride length between points.

range(3, 100, 3)

The second argument is exclusive and will never appear in the sequence. Anything less than it will.

1 Like

yes, the proper indentation was executed but, like i said i would have imagined the desired result would be more 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’,]

never mind, i just got to the part in the exercise where we change that

Why did I have to define a function?

“”" This is a game of battleships “”"
board =
for i in range(5):
board.append([‘O’] * 5)

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

When this will also give same board:

“”" This is a game of battleships “”"
board =
for i in range(5):
board.append([‘O’] * 5)
for i in board:
print (i)

The contents of the board are always changing and the function is a single, re-usable block of code that we can call on every time we want to see the updated board. Otherwise we’d have twenty-five for loops.