7-8/13


#1

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

Here is a question: i don't get why and what difference between lines:


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

def random_col(board):
    return randint(0, len(board[0]) - 1)

First we told to do like: return randint(0, len(board) - 1), but in the next lesson code changes to return randint(0, len(board)[0] - 1)
why? explain me please what it is?


#2

Is row and col always the same? How is a row represented? How many such representations are there? How would one find out the number of columns?


#3

hey ionatan, I'm a complete noob here. Let's decrypt this code together, do you wanna work as a team through a chat system like hangouts or anything like that?

I went step by step like this..
def random_row(board): #is just a function name that will recieve information from the board list *with more lists inside it

----return randit #will obtain one random integer.. as a sample from my list........ which list???
----------------------(0,len(board) - 1) # meaning that it will choose from any of the indexes from your board...

remember our board looks like this (i tried printing it)....

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']]

so if I index them it would be like.....

board = [[0] ['O', 'O', 'O', 'O', 'O'], [1]['O', 'O', 'O', 'O', 'O'], [2]['O', 'O', 'O', 'O', 'O'], [3]['O', 'O', 'O', 'O', 'O'], [4]['O', 'O', 'O', 'O', 'O']]

right??

so that throwing a random sample would be like choosing a row.. right? 'Please.. pro people allowed to step in at any time...

after that the seccond def is pretty much the same:

def random_col(board):

return randint(0, len(board[0]) - 1) but here........ you are being specific...... i want you to choose a random from BOARD INDEX 0!! so we're kinda looking for a colum index... like this

board = [[0] ['O', 'O', 'O', 'O', 'O'], [1]['O', 'O', 'O', 'O', 'O'], [2]['O', 'O', 'O', 'O', 'O'], [3]['O', 'O', 'O', 'O', 'O'], [4]['O', 'O', 'O', 'O', 'O']]

so that's how it chooses a column......... I'm a doctor...... trying to understand this code... its been really funn for me.... but here I got stuck cause I don't understand how this code sets a coordante.. it just picks one item from the first list in board as column reference......

any ideas??


#4

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