如果语句为假,则运行代码

时间:2019-01-28 12:41:46

标签: 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:
        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()  

我添加了一些打印功能,以便可以更轻松地找到错误。 == 21语句的问题在于,尽管不正确,有时也会将其定义为true。

CMD的示例输出:

---1 or 11
---Black Jack!
---Player points: 11

我画了一个A,这表示我得了1分或11分。 IT严格地定义了这些不相等,但是随后定义了其中之一等于21,为什么?让我知道是您需要更多信息还是需要查看所有代码(尽管它不是很漂亮)。

2 个答案:

答案 0 :(得分:2)

csv

答案 1 :(得分:2)

player_points_one是一个非零数字,正在解析为true

此声明对人类读者而言可能是直观的:

if player_points_one or player_points_two == 21

但是编程比人类的直觉更明确。您在此处检查的条件正好是:

  • player_points_one
  • player_points_two == 21

您想要的条件是:

  • player_points_one == 21
  • player_points_two == 21

将会是:

if player_points_one == 21 or player_points_two == 21