0

I have two questions about while-loop! I do not want to waste your time, but I really need help.

1, in my code, while true: and another while is existed. I understand that I can break second while-loop, but how can I break first while-loop at the same time?

2, At the end of code "if stand_hit == 'h'; is there anyway coming back to stand_hit == 's'? I do not want it show print(mycard) things after going through stand_hit == h. I do want to pick up new card or stand immidiately.

Thanks! If you do not make sense my question, please comment!

while True:

player_card = []
player_card.append(player_draw())
player_card.append(player_draw())
print('your cards:',player_card[0]+player_color,player_card[1]+player_color)
player_total = card_dict[player_card[0]]+card_dict[player_card[1]] 

dealer_card = []
dealer_card.append(dealer_draw())
dealer_card.append(dealer_draw())

dealer_cards = []

for i in dealer_card:
    dealer_cards.append(card_dict[i])

dealer_total = sum(dealer_cards)    
player_card = []

stand_hit = input('(S)tand or (H)it?...: ') 

if stand_hit == 's':

    print('Dealer cards:', dealer_card[0]+dealer_color, dealer_card[1]+dealer_color)

        while dealer_total < 17:

            if dealer_total < 11:
                card_dict['A'] = 11
            elif dealer_total > 10:
                card_dict['A'] = 1

            a = random.choice(list(card_dict))
            print('Dealer drew:', a + dealer_color)
            dealer_total += card_dict[a]

            if dealer_total > 21:
                print('You win!')
                player_score += 1
                print('Dealer:', dealer_score, 'Player:', player_score)
                play_again = input('Play again (Y/N)?...: ')
                if play_again == 'y':
                    print()
                    continue
                elif play_again == 'n':
                    print('\n')
                    print('>_')
                    break



elif stand_hit == 'h':
    a_cards = random.choice(list(card_dict))
    print('You drew:', a_cards + player_color) 
    player_total += card_dict[a_cards]
    stand_hit = 's'

    if player_total > 21:
        print('Dealer wins')
        dealer_score += 1
        print('Dealer:', dealer_score, 'Player:', player_score)
        play_again = input('Play again (Y/N)?...: ')
        if play_again == 'y':
            print()
            continue
        elif play_again == 'n':
            print('\n')
            print('>_')
            break
Sajan
  • 1,247
  • 1
  • 5
  • 13

1 Answers1

1

Two methods I can think of to stop the outer loop based on a condition fulfilled in the inner loop.

Pseudo-code:

bln = True

while bln:
    # Do stuff
    while x < y:
        if z == n:
            # Break inner loop, and set bln to False to break outer loop
            bln = False
            break

A second method:

while True:
    bln = False
    # Do stuff
    while x < y:
        if z == n:
            # Break inner loop, and set bln to True for later break statement
            bln = True
            break
    if bln:
        break

Note that with the first method the outer loop's iteration will finish, even when bln is set to True. The second method will break the outer loop abruptly, however.

Tim Stack
  • 3,209
  • 3
  • 18
  • 39