Battleship: 3. Make A List


#1

Here's my code:

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

It says "Oops, try again. It looks like the rows are not represented as lists."
What's wrong?


#2

You have to call a variable with the array of "O" times 5, and board.append that variable.

Then you print it! :smiley:


#3

Like this?

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

Or a different way :question:

Thanks @ragezapper! :smile:


#4

I would suggest putting the variable inside the for loop, because we want to do it a total of 5 by 5. :smiley:

No problem. :smiley:


#5

As in this.

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


#6

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

i finished the exercise but check this out this is a ladder when you run the code
['OOOOO']
['OOOOO', 'OOOOO']
['OOOOO', 'OOOOO', 'OOOOO']
['OOOOO', 'OOOOO', 'OOOOO', 'OOOOO']
['OOOOO', 'OOOOO', 'OOOOO', 'OOOOO', 'OOOOO']
['OOOOO', 'OOOOO', 'OOOOO', 'OOOOO', 'OOOOO']
None
its really kinda of funny


#8

You are printing the new board each time it loops through and appends. I did the same thing the first time I did that exercise. Unindent the fourth line (i.e. "print board") once so that it is no longer inside the for loop.


#9

That's what I got...apparently works

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


#10

this code works

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


#11

thanks actually works
board=[]
for x in range(0,5):
r=['O']*5
board.append(r)
print board


#12

thanks,it works.oooooooooooooo


#13

Something I still don't fully understand about this exercise (despite the helpful info above). I was getting an error as my ["O"] * 5 line was outside of the for loop, stored in a variable, like so:

board = []

def fillBoard(gameBoard):
    ocean = ["O"] * 5
    for i in range(0,5):
        gameBoard.append(ocean)
    return board

print(fillBoard(board))

When run, it was coming up with an error saying the ocean variable was being referenced 5 times, which might cause problems later. Of course, I have just this second re-loaded the page and the error has gone away, but reading the advice above it sounds like it would be better if the ocean variable declaration was inside the loop. Is there any good reason for this? It seems like you're asking the computer to reassign each time, which to my mind seems wasteful when you can just declare the variable once and populate each list with the data inside... Can anybody help me understand what the benefit is?

Thanks!


#15

I understand this, but doesn't range(0,5) have to be a reference to something? I thought for example, if you reference a list that doesn't have enough elements like

board = ["0", "0",]

range(6)

then it would just return as many elements there are in the list (2 in this case). So how does the for loop get enough items to iterate through?


#16

This prints 5 sub lists in the board list. Are we supposed to do it like that or print 5 individual lists with all "O"s?


#17

Nothing wrong with in it .
My code as follows

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

#18

this is what it should be

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

#19

Segue solução para se criar uma grade 5 x 5, inicializado com 'O's em todas as posições, e armazenando-os em board.

script.py

board = []
for x in range(5):
board.append(["O"] * 5)
print board


#20

change print "O" to

rows = 5* ["O"]
print rows
board.append(rows)

instead since its asking for a lists and not a string which you are giving


#21

i don't understand, the problem.. i make this code and i have some problems with... please test my code and speak where ai do something wrong ...

board = []

def create(lst):
    
    ls = []
    
    for i in range(5):
        lst.append("O")
        ls.append(lst)
    
    #return lst
    return ls

"""    
def create_sub(lst):
    
    ls = []
    
    for i in range(0,5):
        ls.append(lst)

    return ls
"""

print create(board)

print

for i in range(5):
    print board

#22

Let's go over what they're asking us to do:

  1. Create an empty list called board
  2. Create a loop that will iterate that list 5 times
  3. Insert 5 'O''s into each of those lists

The instructions never asked us to print our results, but to make sure we're doing it correctly, it's always good to have a visual guide. Make sure you delete the print statement before you move on to the next lesson!!

Now, here's the code that will give you the proper result:

board = []

for x in range(5):
    board.append(['O'] * 5)
    print board[x]

This prints:

['O', '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

Nice and neat.

Let's go over what the program is reading, and how it outputs this information:

board = []

Python: Great, this guy forgot to fill his list!

board = []

for x in range(5):

Python: Okay, so they want to loop with a variable called x, and do that 5 consecutive times.

board = []

for x in range(5):
    board.append(['O'] * 5)

Python: So, for all 5 instances, they want to create 5 items called 'O' in list format, and then add it to the board list.

board = []

for x in range(5):
    board.append(['O'] * 5)
    print board[x]

Python: Alright, I'll go through the for loop five times, and every time I do, I'm gonna print the board list, and inside that list, I'm gonna print the new and improved x variable (which just so happens to be 5 'O''s)

There you have it. Hopefully that helps anyone who was still having trouble understanding this part.

Moderators/Seasoned Python Veterans: Feel free to judge and correct my "Python Translation" as I'm still new to Python.