Home > Software design >  Trying to make a blackjack game kind of
Trying to make a blackjack game kind of

Time:12-17

Summarize the problem

I am trying to make a simple blackjack program that picks two numbers for the dealer 1-11 and two numbers for the player 1-11 also. I want to make a loop that checks if the player is hitting or staying based on what they input, hit or stay. If they hit I want it to add a card to the player deck and give them the option to hit or stay again.

If they stay I want the dealer to check a set of rules and those rules are:

1: does the dealer have less than 17 and does it have less than the player 1a: if it does have less than 17 and less than the player it will draw a new number 1-11 and add it to the dealer hand 1b: it will then run through the loop of checking if it has the following conditions

2: does the dealer have 17-21 and does it have more than the player 2a: if it has any number 17-21 and it has more than the player then it will print a win message

3: does the dealer have over 21 3a: if it has over 21 then it will print a house busted message

4: does the dealer have more than the player 4a: if the dealer has more than the player (but less than 21 because of the previous check) then it will print a house wins message

So that is what I am trying to accomplish with the stay command and so far i haven't found any issues with it.

My hit command however will not register, even if I don't type stay.. I'm trying to debug it by having it print a message and end the loop when i type hit but it still runs through the stay commands regardless, I think it could be an issue with my input converting to a string.

Don't be too harsh on me lol I only taught myself how to code last week and started with python.

Here is my code where I am having issues:

'''

                    else:

                        hitstay = True

                        while hitstay:
                            action = str(input("hit or stay? "))
                            dealer = sum(dealer_cards)
                            player = sum(player_cards)
                            if action == 'stay' or 'Stay':
                                if dealer < 17 and dealer < player:
                                    dealer_cards.append(random.randint(1, 11))
                                    dealer = sum(dealer_cards)
                                    print(f"Dealer pulls {dealer_cards[-1]}\n"
                                          f"Dealer now has {sum(dealer_cards)}")
                                    if 17 <= dealer <= 21 and dealer > player:
                                        print(f"House won! Dealer cards: {dealer_cards}, {sum(dealer_cards)}\n"
                                              f"Player cards: {player_cards}, {sum(player_cards)}")
                                        playing = False
                                        hitstay = False
                                    elif dealer > 21:
                                        print(f'House busted! Dealer cards: {dealer_cards}')
                                        playing = False
                                        hitstay = False
                                    elif dealer > player:
                                        print(f'House wins, Dealer cards {dealer_cards}, {sum(dealer_cards)}\n'
                                              f'Player cards: {player_cards}, {sum(player_cards)}')
                                        playing = False
                                        hitstay = False
                            if action == 'hit' or 'Hit':
                                print('command hit')
                                playing = False
                                hitstay = False

'''

CodePudding user response:

Instead of:

if action == 'stay' or 'Stay':

do:

if action == 'stay' or action == 'Stay':

or:

if action in ('stay', 'Stay'):

or best of all:

if action.lower() == 'stay':

The first version doesn't work because it's interpreted as:

if (action == 'stay') or 'Stay':

which is the same as:

if (action == 'stay') or True:

which is the same as:

if True:
  • Related