python嵌套if语句不起作用

时间:2016-10-09 20:51:47

标签: python if-statement

我尝试使用pygame制作游戏,并且我已经设置了一些我已经测试过的功能,并且它们运行良好。但是,在下面的代码中,当我添加一个print语句来显示我的代码是否有效时,它会在第一个而不是第二个上打印。有什么帮助吗?

for square in row:
    tile_x = row.index(square)
    # print statement works here
    if self.x_pos - tile_x >= -4 and self.x_pos - tile_x <= 4:
        tile_x = 4 - (self.x_pos - tile_x)
        tile_y = 4 - (self.y_pos - tile_y)
        if square == 'G':
            display('Grass',tile_x,tile_y)
            # print statement doesn't work here
        elif square == 'T':
            display('Tree',tile_x,tile_y)
        elif square == 'B':
            display('Bush',tile_x,tile_y)
        elif square == 'R':
            display('Rock',tile_x,tile_y)
        elif square == 'S':
            display('Stone',tile_x,tile_y)
        # display function has been tested, and it works fine

1 个答案:

答案 0 :(得分:0)

我刚想到了什么

我正在使用row.index(square),它正在找到第一个'g'方格,并使用的tile_x 第一个方块。这意味着它认为我正在寻找的正方形不在地图上显示的正确范围内,因此第一个if语句返回False并且它不会显示或打印出来。

相关问题