如何解决此列表超出范围的问题?

时间:2019-06-27 13:18:52

标签: python

我正在制作一个游戏,其中玩家可以在8x8网格上移动,但是却出现错误,其值超出范围。

这是我的代码:

def player_movement():
    grid0 = [] 
    grid1 = []
    i = 0
    n = 0
    while i < 8: #this makes the grid
        grid0.append("0")
        i += 1
    while n < 8:
        grid1.append(grid0.copy())
        n += 1

    grid1[0][0] = "X" # this places the player on the top left of the grid

    for l in grid1:
        print(l)

    while True:
        player_movex = int(input("Move right how many?"))# instructions to move the player
        player_movey = int(input("Move down how many??"))

        for y, row in enumerate(grid1): #this finds the player on the grid
            for x, i in enumerate(row):
                if i == "X":
                    grid1[y][x], grid1[y + player_movey][x + player_movex] = grid1[y + player_movey][x + player_movex], grid1[y][x]

        for j in grid1: #prints out the grid in the 8x8 format
            print(j)

并且我输入的是列表范围内的值,即0-7。

这是我的屏幕上出现的错误:

Traceback (most recent call last):
  File "D:\Python\Treasure Hunt game.py", line 83, in <module>
    player_movement()
  File "D:\Python\Treasure Hunt game.py", line 78, in player_movement
    grid1[y][x], grid1[y + player_movey][x + player_movex] = grid1[y + player_movey][x + player_movex], grid1[y][x]
IndexError: list index out of range

2 个答案:

答案 0 :(得分:0)

原因是即使执行运动后仍执行循环。

def player_movement():
    grid0 = [] 
    grid1 = []
    i = 0
    n = 0
    while i < 8: #this makes the grid
        grid0.append("0")
        i += 1
    while n < 8:
        grid1.append(grid0.copy())
        n += 1

    grid1[0][0] = "X" # this places the player on the top left of the grid

    for l in grid1:
        print(l)

    while True:
        player_movex = int(input("Move right how many?"))# instructions to move the player
        player_movey = int(input("Move down how many??"))

        done = False
        for y, row in enumerate(grid1): #this finds the player on the grid
            for x, i in enumerate(row):
                if i == "X":
                    print(y, x)
                    grid1[y][x], grid1[y + player_movey][x + player_movex] = "0", "X"
                    done = True
                if done == True:
                    break
            if done == True:
                break

        for j in grid1: #prints out the grid in the 8x8 format
            print(j)

player_movement();

答案 1 :(得分:0)

我宁愿编写如下代码:

def player_movement():
    n = 8
    grid = [['0'] * n for _ in range(n)]
    m = 'x'
    grid[0][0] = m  # this places the player on the top left of the grid

    for i in grid:
        print(i)

    while True:
        # instructions to move the player
        player_movex = int(input("Move right how many? "))
        player_movey = int(input("Move down how many?? "))

        move(grid, m, n, player_movey, player_movex)
        for j in grid:  # prints out the grid in the 8x8 format
            print(j)


def move(grid, m, n, move_y, move_x):
    for y, row in enumerate(grid):  # this finds the player on the grid
        for x, i in enumerate(row):
            if i == m:
                a, b = y + move_y, x + move_x
                if a >= n:
                    print(f'Sorry, move {move_y} down will out of range!\n')
                    return
                if b >= n:
                    print(f'Sorry, move {move_x} right will out of range!\n')
                    return
                grid[y][x], grid[a][b] = grid[a][b], grid[y][x]
                return


player_movement()