首页 > 解决方案 > 为什么 if 语句正在执行但条件为假?

问题描述

即使变量bullets_fire包含 boolean False,下面的命令也会被执行。我还为嵌套在 if 语句中的 bullets_fire 添加了 print 语句,它输出False,为什么要执行它呢?

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_SPACE and not bullets_fire:
        print(bullets_fire)
        bullets_fire = True
        bullets_x = player_x
        bullets_y = player_y

标签: pythonif-statementprintingboolean

解决方案


您的条件包含not在布尔变量之前。仅当event.key == pygame.K_SPACE评估为Truebullets_fire评估为时才会执行False,因为not False与 相同True


推荐阅读