Python Forum

Full Version: 2d List not returning
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey, Im trying to get this function to return a value like [x][y]
but it keeps giving me "line 54, in move
return [new_row][new_col]
IndexError: list index out of range."
(sorry for not knowing the correct terms for some of this stuff, im just starting out.)

player_row = 0
player_col = 0

def move(direction):
    new_col = player_col
    new_row = player_row
    if direction == "up":
        new_col += -1
    elif direction == "down":
        new_col += 1
    if direction == "left":
        new_row += -1
    elif direction == "right":
        new_row += 1
    return [new_row][new_col]
print(move("down"))
It gives me a [x] value if i remove either new_row or new_col from the return,
player_row = 0
player_col = 0

def move(direction):
    new_col = player_col
    new_row = player_row
    if direction == "up":
        new_col += -1
    elif direction == "down":
        new_col += 1
    if direction == "left":
        new_row += -1
    elif direction == "right":
        new_row += 1
    return [new_col]
print(move("down"))
and it will give me ([x],[y]) if i return "return [new_row], [new_col].
is there any way to get it to return [x][y] without the index error? Thanks.
[x][y] is not a valid python object. [x] is one-element list as well as [y]. ([x], [y]) is two-element tuple, each element being one-element list.
maybe explain what you want to achieve
(Sep-22-2020, 05:03 PM)buran Wrote: [ -> ][x][y] is not a valid python object. [x] is one-element list as well as [y]. ([x], [y]) is two-element tuple, each element being one-element list.
maybe explain what you want to achieve

Gotcha man, thank you!