0

According to my understanding when I should run the the while loop in the main body and calls the hit_or_stand(deck,hand) function, the code should run indefinitely times around the while loop(in the function) when I choose h but to my surprise the following code run in the following way:

1- Asks for h or s ?
   I type h
2- show_some(player_hand,dealer_hand)
3-if player_hand.value > 21:
            player_busts(player_hand,dealer_hand,player_chips)
            break

Do not you think the code should run indefinitely around 1st step as I am always choosing h and to my understanding it should remain in the loop of hit_or_stand() function.

def hit_or_stand(deck,hand):
    global playing

    while True:
        x = input("Would you like stand or hit? Enter 'h' or 's'")
        if x[0].lower() == 'h':
            hit(deck,hand)
        elif x[0].lower() == 's':
            print("player stands. Dealer is playing. ")
            playing = False

        else:
            print("Sorry, please try again.")
            continue
        break

Main Body

 while playing: # recall this variable from our hit_or_stand function

        hit_or_stand(deck,player_hand)
        show_some(player_hand,dealer_hand)
        #if player hand exceeds 21, run player busts() and break out of the loop
        if player_hand.value > 21:
            player_busts(player_hand,dealer_hand,player_chips)
            break
  • What is `player_hand.value`? We need to see more code. Please create a [mcve] that demonstrates the problem. Show us enough code that we can run the program ourselves but strip out anything not directly related to the problem. For instance, hard code the user input and remove branches that aren't run. – John Kugelman Jan 03 '19 at 20:50
  • What do you think that final `break` does in your `while True` loop? – cdarke Jan 03 '19 at 20:54
  • I am building a BlackJack Game, The player_hand defines the object of cards player has chosen from the deck. – Muneeb Bilal Jan 03 '19 at 21:13
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response). – martineau Jan 03 '19 at 21:35

1 Answers1

-1

The break instruction cancels your while-loop. (https://docs.python.org/2.0/ref/break.html).

The while True loop in the hit_or_stand function will only run once, because of the final break statement. Whereas the loop in the main body will quit when the if statement is reached.

ZeroQ
  • 109
  • 3
  • How so? I am choosing h everytime, to my understanding loop should stick around the if statement . Break statement will start implementing if both the above statements will not meet the condition, In my case I am always choosing h which mean 1st condition is being met. – Muneeb Bilal Jan 03 '19 at 21:37