0
我有以下循环:如果语句始终计算为真,即使是假的
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_tiles
和enemy_tiles
形式的字典是字典的形式{<tile_id> : <tile>, ... }
。
循环总是返回tile_id
无论neighbour
是enemy_tiles
与否,但不若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
作用一样。
如有疑问,请添加括号。 –
尝试'如果邻居不在enemy_tiles中,并且tile [“player”]为None:' - 习惯性Python FTW。 – thefourtheye
@thefourtheye我知道'None'比'== None'更加pythonic,但是为什么'=='不行? –