双while循环中断并跳转到while循环问题

我有两个关于while循环的问题!我不想浪费你的时间,但我真的需要帮助。

1,在我的代码中,while true: 还有一个 while 存在。我知道我可以打破第二个while循环,但我怎样才能同时打破第一个while循环?

2,在代码“if stand_hit == 'h'; 的末尾有没有回到stand_hit == 's'?我不希望它在通过stand_hit == h 后显示打印(mycard)的东西。我愿意想拿起新卡或立即站立。

谢谢!如果您对我的问题没有意义,请发表评论!

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


慕村225694
浏览 103回答 1
1回答

蝴蝶不菲

根据内循环中满足的条件,我可以想到两种方法来停止外循环。伪代码:bln = Truewhile bln:&nbsp; &nbsp; # Do stuff&nbsp; &nbsp; while x < y:&nbsp; &nbsp; &nbsp; &nbsp; if z == n:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Break inner loop, and set bln to False to break outer loop&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bln = False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break第二种方法:while True:&nbsp; &nbsp; bln = False&nbsp; &nbsp; # Do stuff&nbsp; &nbsp; while x < y:&nbsp; &nbsp; &nbsp; &nbsp; if z == n:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Break inner loop, and set bln to True for later break statement&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bln = True&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; if bln:&nbsp; &nbsp; &nbsp; &nbsp; break请注意,使用第一种方法,外部循环的迭代将完成,即使bln设置为 True。然而,第二种方法会突然中断外循环。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python