1

The function gboard is suppose to return a nested list in column major order. I have gotten it to do this, but I am not sure of how to set each value in the list equal to 0 (or the variable I set equal to 0 in another function called constants.dead.) If anyone has any idea of how I could do this I would appreciate the help.

def gboard(height, width):
    """ Returns: Nested list in column-major order representing a game board.  All the cells in the board are in the set to 0.

    Parameter height: number of rows in the board
    Parameter width: number of columns in the board
    Precondition: height and width are positive ints
    """
    list = []
    for y in range(width):
        list.append([])
        for x in range(height):
            list[y].append(x)
    return list
  

The output of the function for example is this:

>>> gboard(4,3)
[[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]]

But I would rather it be:

>>> gboard(4,3)
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
3
  • my suggestion: board = [width*[0] for _ in range(height)], and please do nut use list as variable name... Commented Jul 22, 2020 at 19:31
  • ah okay i wont use list but i am still confused as to what board is for you and where you get 19 from. Commented Jul 22, 2020 at 19:34
  • that would be the return value of your function... return board, ...added as an answer. Commented Jul 22, 2020 at 19:35

2 Answers 2

1

i suggest you use a list-comprehesion:

def gboard(height, width):
     return [height*[0] for _ in range(width)]

that would produce

gboard(4, 3) 
# [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Sign up to request clarification or add additional context in comments.

Comments

0

change list[y].append(x) to list[y].append(0)

strange it didn't worked for you. apparently this code worked for me:

def gboard(height, width):
    """ Returns: Nested list in column-major order representing a game board.  All the cells in the board are in the set to 0.

    Parameter height: number of rows in the board
    Parameter width: number of columns in the board
    Precondition: height and width are positive ints
    """
    list = []
    for y in range(width):
        list.append([])
        for x in range(height):
            list[y].append(0)
    return list
  
print(gboard(4, 3))

output:

[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

1 Comment

Thanks but I tried this and it does not work I get a Type Error: TypeError: append() takes exactly one argument (0 given) And this happens if i put anything within those parentheses besides x. (any int value)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.