我有两个关于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
1 回答
蝴蝶不菲
TA贡献1810条经验 获得超4个赞
根据内循环中满足的条件,我可以想到两种方法来停止外循环。
伪代码:
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
第二种方法:
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
请注意,使用第一种方法,外部循环的迭代将完成,即使bln设置为 True。然而,第二种方法会突然中断外循环。
添加回答
举报
0/150
提交
取消