0

I'm very new to Python and to test my knowledge, I'm trying to build a Black Jack game. The code is rather long at this points, everything works fine, the following code is the only problem:

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()  

I added some print functions so that I could easier find the bug. The problem with the == 21 statement is that it is sometimes defined as true, despite not beging true.

Example output from CMD:

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

I drew and an A, which means that I have either 1 or 11 points. IT corectly defines that these are not equal, but it then defines that one of these is equal to 21, why is that? Let me know if you need more information or need to see all the code (though its not very pretty).

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69

2 Answers2

2
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
Santhosh Kumar
  • 543
  • 8
  • 22
2

player_points_one, being a non-zero number, is resolving to true.

This statement may make intuitive sense to a human reader:

if player_points_one or player_points_two == 21

But programming is a bit more explicit than human intuition. The conditions you're checking here are precisely:

  • player_points_one
  • player_points_two == 21

Whereas the conditions you want are:

  • player_points_one == 21
  • player_points_two == 21

Which would be:

if player_points_one == 21 or player_points_two == 21
David
  • 208,112
  • 36
  • 198
  • 279