Python If语句即使为false也运行代码

Python If语句即使为false也运行代码,python,python-3.x,if-statement,Python,Python 3.x,If Statement,我对Python非常陌生,为了测试我的知识,我正在尝试构建一个Black Jack游戏。此时代码相当长,一切正常,以下代码是唯一的问题: def player_round_end(): global player_points if player_points_one == player_points_two: # This works just fine elif player_points_one != player_points_two:

我对Python非常陌生,为了测试我的知识,我正在尝试构建一个Black Jack游戏。此时代码相当长,一切正常,以下代码是唯一的问题:

def player_round_end():
    global player_points
    if player_points_one == player_points_two:   
        # This works just fine
    elif player_points_one != player_points_two:
        print(f"---{player_points_one} or {player_points_two}")
        if player_points_one or player_points_two == 21: #This is the probelem
            print("---Black Jack!")

            if player_points_one == 21:
                player_points = player_points_one
            else:
                player_points = player_points_two

            print(f"Player points: {player_points}")
            dealer()      
        elif player_points_one and player_points_two > 21:
            print("You lost!")
            new_game()  
我添加了一些打印功能,以便更容易找到bug。==21语句的问题在于它有时被定义为true,尽管它不是true

CMD的输出示例:

---1 or 11
---Black Jack!
---Player points: 11
我画了个A,也就是说我得1分或11分。它正确地定义了这些不相等,但它又定义了其中一个等于21,为什么?如果您需要更多信息或需要查看所有代码,请告诉我,尽管它不是很漂亮。

player\u points\u one作为一个非零数字,正在解析为true

if player_points_one or player_points_two == 21: ## Meaning player_points_one exists or player_points_two == 21

if player_points_one == 21 or player_points_two == 21: ## Correct solution
这句话对人类读者来说可能有直观的意义:

if player_points_one or player_points_two == 21
但是编程比人类的直觉要明确一些。您在此处检查的条件正是:

球员得分1分 球员得分=21 鉴于您需要的条件是:

球员得分=21分 球员得分=21 这将是:

if player_points_one == 21 or player_points_two == 21

如果player\u points\u one==21或player\u points\u two==21?谢谢,这就是问题所在!是的,你说得对,谢谢!就我所记得的,第一个语句在VBA中是正确的,但在Python中显然不起作用。如果它是正确的,请随意选择它作为结束此问题的正确答案:I will!。但是,在我选择正确的解决方案之前,从帖子创建起还有一个10分钟的计时器:@DennisChristiansen为什么这个答案不被接受如果它有效我不知道,我什么都没做?就我记忆所及,我确实认为你是第一个给出正确答案的人?谢谢你的解释,这很有道理!正如我前面提到的,我对Python是完全陌生的,但在VBA中你实际上可以同时做这两件事,所以我只是假设它在Python中的工作方式是相同的。@DennisChristiansen从来没有假设任何东西的工作方式都与VBA相同-