If语句总是被计算为true,即使它是fals

2024-04-19 15:40:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下循环:

    for tile_id, tile in corner_tiles.items():
        for neighbour in tile["neighbours"]:
            if not (neighbour in enemy_tiles) and tile["player"] == None:
                return tile_id

其中: tile

{'counters': <number of counters on tile or None>, 
 'player': <player id of the player who holds the tile or None>,
 'neighbours': <list of ids of neighbouring tile>
}

corner_tilesenemy_tiles{<tile_id> : <tile>, ... }形式的词典。你知道吗

你知道吗

不管neighbour是否在enemy_tiles中,循环总是返回tile_id,但如果tile["player"]不是None,则不会返回。我想弄清楚这一点已经有一段时间了,但似乎无法发现哪里出了问题。你知道吗

编辑:

我也尝试过:

for tile_id, tile in corner_tiles.items():
    if tile["player"] is None:
        for neighbour in tile["neighbours"]:
            if neighbour not in enemy_tiles:
                return tile_id

它们的作用是一样的。你知道吗


Tags: ofinnoneidforifnotitems
1条回答
网友
1楼 · 发布于 2024-04-19 15:40:09

在经历了巨大的压力之后,我发现了错误背后的简单原因,我正在为错过它而自责。。。你知道吗

我的循环应该做的是确保所有neighbours都不在enemy_tiles中。当然,它几乎每次都被评估为true,因为检查的第一个neighbour不在enemy_tiles中的概率要高得多,因此该语句为true,并且在只检查一个neighbour之后返回tile_id。你知道吗

相关问题 更多 >