Python 5. Battleship! Custom Print -- I think I should have failed, not passed

My print out display shows:

O O O O O O O O O O O O O O O O O O O O O O O O ONone

Yet I passed? With this?

board = []
for spot in range(5):
    board.append(["O"]*5)
    
for line in board:
    for os in line:
        print os,

def print_board(board):
    for row in board:
        print row

board

To β€˜PASS’ should I not have had 5 rows that look like this or something similar:

OOOOO
OOOOO
OOOOO
OOOOO
OOOOO

instead of the:

O O O O O O O O O O O O O O O O O O O O O O O O ONone

???

Am I going crazy and expecting too much here? Or is this there something missing in the test on this one?

Yeah, I agree, that’s not right. The output for this step should look 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

The formatting debris gets cleaned up in later lessons, so it’s OK to look a bit messy. That said, the changes to correct this are minor!

The goal here is to get the nested lists to each print on their own line, nothing more. I think that’s what got you, as you’re attempting to get the fully formatted and clean output already!

So, with the goal in mind, you can scrap the β€˜for line in board’ section, as we only need to define the function and print each list. To do that we simply need a for loop that counts up through the items (lists) in each position in β€˜board’. I used a variable β€˜x’ within the function to track and increment the position, but someone else might have a cleaner way to solve the issue.

So, try removing the whole β€˜for line in board:’ chunk of code and changing your β€˜print row’ to something like this:

    print board[x]

This change will simply print the item (a list in this case) at the position β€˜x’ in the list β€˜board’. Since each item is a list we get the output I showed you at the beginning of this reply!

You’ll need to add a variable β€˜x’ to the function and build the increment process within the loop, but that should be easy!

Hope that helps!

1 Like

Thank you codyroche.

I have flagged & reported. Hopefully they catch it before too many more go along the python brick road tangent :smile:

For anyone else reading this… Be careful!!!

Make sure your print outs are right. Don’t just go for a PASS. Go for your own personal ROCKSTAR badge.

2 Likes

I’m very confused what CodeAcademy wants.

board =
for i in range(0,5):
board.append([β€œO”])

def print_board(board):
for row in board:
print board

print_board(board)

Which is what it sounds like the lesson is requesting and even prints the way I want it to

[[β€˜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

But I still get the error β€œOops, try again. It looks like you function didn’t print out the correct response!”

Any help would be appreciated.

1 Like

You have each [β€˜O’] as a separate list, not 1 list on each row. board.append([β€˜O’] * 5)

Excellent! Thanks! That fixed it

I don’t believe I should have gotten this correct either. My code it below:

board =
for i in range(5):
’ board.append(5 * [β€œO”])
for z in board:
’ for y in z:
β€˜β€™ print y,
’ print
def print_board(board):
’ for i in range(len(board)):
β€˜β€™ print board[i]

I used ’ to help show indents. I get the result of
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 is what I wanted, but I don’t think my code reflects what my result is though. Can anyone confirm this?

Yes, I just hijacked the thread.

The current Python course is one of the β€œold” format courses and is no longer being updated. In the not too distant future, I’m not sure of the exact timeframe, you can expect to see Python appear in the β€œnew” course format. At that point we can all start reporting bugs again. :slight_smile: :bug:

1 Like

Same here! Over and over again!

Try copy this and paste on your script.py. I failed many times and this worked! I actually mistook number 0 for caplock of letter β€˜O’ . Hope it helps!
board =
for i in range(0,5):
board.append([β€˜O’]*5)

def print_board(board):
for row in board:
print row
print print_board(board)

1 Like