Danger, Will Robinson!


#1



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

My code is working fine , just recalling back from the beginning of the game , I've got some questions

First

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

I understand that his function removed the commas and the ' ' .. i just don't get how it managed to get them into 5 lines under each others ?

Second

def random_row(board):
    return randint(0, len(board) - 1)

Here , the length is considered "5" , because the outer list contains 5 lists ?

Third

print board[2][3]

The example above prints out "O", the element in the 3rd row and 4th column... can't get it at all
even when i tried in the code row 2 column 2 .. i get the X at the third column ,third row ..
isn't every row was originally a list inside the bigger list ?

Last

else :
    print "You missed my battleship!"
    board[guess_row][guess_col] = "X"

how did we assign the board list 2 values in this command?

Thanks in advance <3


#2

well, your board is a list (lets call this outer list) containing 5 other lists (let call them inner lists)

each inner lists contains 5 O's, by looping over the outer list, you get access to the inner list which you print as string using join.

well, python prints on a new line every time a print keyword is reached. Here a print statement is reached 5 time (given the loop, so each will print on a separate line)

yes

not sure what you mean, but keep in my that lists are zero indexed based, where humans start counting at one (list: 0 till 4 (inclusive), humans: 1 till 5)

we don't? you assign one value (X) to a specific coordinate


#3

Thanks alot for your clarifications , and yes i was just missing that the lists are zero indexed
much appreciating your help


#4

its also important to realize that the way we store data (lists) and how we represent the data to user (board) doesn't always match


#5

am sorry i didn't get this ,


#6

we show this nice board to the user playing our game, but we store the data in lists. Its just the way we program and store data doesn't match with what we show the user

not very important


#9

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