Help on this lesson


#1

Here is my link, Error code and my code.

It works fine does what it's supposed to do but I get this error and can't proceed.

https://www.codecademy.com/en/courses/python-beginner-en-4XuFm/0/5?curriculum_id=4f89dab3d788890003000096#

board = []

for x in range(0, 5):
    board.append(['O'])
for y in board:
    def print_board(board):
        for row in board:
            return board
    print print_board(board)

So please explain what is going on.
why it is happening, and what is the solution.
This way I will understand how to not make the same mistake.
Thank you.


#2

This section killed me! For some reason, I kept trying to skip ahead to the final board and the site rejected everything I did. By the time I realized I was jumping ahead and took it step-bystep, I had found quite a few ways to work with these methods!

Here's my code for that portion of the game:

board = []

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

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

print_board(board)

#3

Works but that statement

board.append(["o"] * 5)

kills me I just dont under stand what it is doing and why.


#4

As I understand it (there may be a more accurate way to describe it than I'm capable of),

the expression
(["O"] * 5
creates a list that looks like this:
[ "O", "O", "O", "O", "O"]

the expression:
for x in range(0, 5):
means that for every index position in that range (remember, it starts at 0 and goes up to, but not including, 5).

one of these:
[ "O", "O", "O", "O", "O"]
gets appended to the board list,

so you end up with the empty board list getting five copies of the new list appended to it, and it becomes a "list of lists":

board = [[ "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 part joins the strings and removes the " ", so we nice neat rows and columns:

OOOOO
OOOOO
OOOOO
OOOOO
OOOOO

I hope that helps!

Mark


#5

Yes it does Thank you


#6

I wonder why the result is
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
instead of OOOOOOOOOOOOOOOOOOOOOOOO
Could you please explain this? Thanks.


#7

This was a two step process. In the first part (step 5, custom print), we created a function that printed a row of [ "O", "O", "O", "O", "O"] for every item in the board list. There are 5 items in board, so we get 5 rows:

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

which 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"]

Then, in Printing Pretty, we modified that function using .join to put the strings within each sub-list in board together without the spaces, brackets and " " (putting the " " before .join made each row of 5 "O", strings into one long string):

def print_board(board): 
    for item in board:
        print " ".join(row)
    
print_board(board)

For each item in board (the five lists containing "O"s), it printed the separate strings in that item joined together as one longer string, which yields:

OOOOO
OOOOO
OOOOO
OOOOO
OOOOO

Below is an initial version of the first part of the game in which the row element is more obvious. I think I changed it to streamline it.

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

The row variable is the thing that gets appended to the board list with each iteration. The second version of the function just rolls it into the append statement.

I hope that helps!


#8

this statement is actually multiplying o character 5 times during a single run of the loop
so when for statement runs 1 time it creates list with 5 o characters


#9

each time you run a print function, it ends the output with a newline character, unless you tell it not to.


#10

It does help. Thank you for the explaination.


#11

Thank you very much for the answer.