Home > OS >  I cant close my while-loop in a Blackjack game
I cant close my while-loop in a Blackjack game

Time:10-14

I am making a Blackjack game in Python, and I am using a while loop. In my code I got an input with an if statement where if the player choose to play another round (types in "Y"), the game starts again (which I got no problem with). My problem is that if the player types in "N" (for no), the game still reruns. What I am trying to do is that when the player types in "N", the game is ended. What is wrong in my code? I am stuck and can't figure it out.

Also as you can see in my code, I use the closed = True several times in the while-loop and it works (for example if the player gets a score of 21, the game ends). The only time the closed = True does not work is in the elif play_again.lower() != "Y": closed = True part of my code.

closed = False
while not closed:

    if player_total == 21:
        print("\nCongrats, the player got a Blackjack!")
        closed = True

    else:
        input_answer = int(input("\n1 - Hit \n2 - Stand \n \nDo you wish to Hit or Stand? "))

        if input_answer == 1:
            print("You chose to Hit.")
            give_cards(player_hand, 1)
            player_total = bjm.calculate_hand_value(player_hand)
            print(f'\nThe player has got a new card: {player_hand[2]}. \nThe player currently has {", ".join(player_hand)},'
                  f' with a total value of {player_total}.')

            if player_total > 21:
                print("Busted! You got a total value over 21.")
                closed = True

        elif input_answer == 2:
            print("\nYou chose to Stand.")
            print(f'The dealers cards are: {" and a ".join(dealer_hand)}, with a total value of {dealer_total}.')
            print(f'The players cards are {", ".join(player_hand)} with a total value of {player_total}.')
            print_result(player_hand, dealer_hand)

            play_again = input("\nDo you want to play another round? Y/N: ")

            if play_again.lower() != "N":
                print("You wanted to play another round, you will be dealt two new cards.\n")

                player_hand.clear()
                dealer_hand.clear()

                give_cards(player_hand, 2)
                give_cards(dealer_hand, 2)

                print(f'\nThe players new cards are: {" and a ".join(player_hand)}, with a total value of {player_total}.')
                print(f'The dealers new cards are: {" and a ".join(dealer_hand)}, with a total value of {dealer_total}.')

            elif play_again.lower() != "Y":
                closed = True

closed = True 

CodePudding user response:

I'm not sure why you are using double negatives. Try this instead:

while True:
    ...
    elif play_again.lower() != "Y":
        break #instead of closed=True

CodePudding user response:

A better way than what you are using right now to end the loop is to simply replace your condition in closed = True with break.

Additionally, the reason your code isn't working is because you are trying to compare the .lower() (which will always give you a lower case letter) with a capital case letter, which means that this condition:

            elif play_again.lower() != "Y":
                closed = True

will never be true.

replace it with "y" instead of "Y"

  • Related