2d List not returning - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: 2d List not returning (/thread-29842.html) |
2d List not returning - DariusKsm - Sep-22-2020 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. RE: 2d List not returning - buran - Sep-22-2020 [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 RE: 2d List not returning - DariusKsm - Sep-22-2020 (Sep-22-2020, 05:03 PM)buran Wrote: Gotcha man, thank you! |