You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Blackjack游戏while循环未退出问题求助(禁用break)

Blackjack游戏循环未正常退出的问题排查与修复

核心问题

  1. Ace值调整后未更新分数
    当用户手牌里的11(Ace)导致爆牌时,代码把11改成了1,但没重新计算user_score。比如实际分数已经降到21以下,user_score还是原来的爆牌数值,逻辑判断全错,循环自然退不出去。
  2. 状态更新后仍执行冗余代码
    就算把should_continue设成了False,循环里后面的line_gap()和询问要牌的代码还是会跑,程序还会问你要不要牌,根本没法退出。
  3. 最终结算时电脑分数没更新
    final_score_calculation函数里给电脑补了牌,但没重新算computer_score,最后显示的分数是错的。

修复方案

  1. 改完Ace值立刻更新分数
    把11换成1之后,马上调用calculate_score更新user_score或computer_score,保证后续判断用的是最新分数。
  2. 用条件跳过冗余代码
    一旦设置了should_continue = False,就跳过后面的询问要牌逻辑,别再执行无效操作。
  3. 结算前更新电脑分数
    在final_score_calculation里给电脑补完牌后,重新计算computer_score。

修正后的完整代码

import random

def deal_card():    
    cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
    random_index = random.randint(0, len(cards)-1)
    random_card = cards[random_index]
    return random_card

def want_another_card():
    want_card = input("Want another card type 'y' for yes and type 'n' for no.")
    return want_card

def line_gap():
    print("------------------------")

user_card = []
computer_card = []

for x in range(2):
    user_card.append(deal_card())
    computer_card.append(deal_card())

def calculate_score(card_list):
    score = sum(card_list)
    return score

user_score = calculate_score(user_card)
computer_score = calculate_score(computer_card)

# final score function
def final_score_calculation():
    while calculate_score(computer_card) < 17:
        computer_card.append(deal_card())
    # 更新电脑最终分数
    global computer_score
    computer_score = calculate_score(computer_card)
    
    line_gap()
    print(f"Your final hand: {user_card}, final score: {user_score}")
    print(f"Computer's final hand: {computer_card}, final score: {computer_score}")
    
    if user_score > computer_score and user_score < 21:
        print("User wins")
    elif computer_score > user_score and computer_score < 21:
        print("Computer wins")
    elif computer_score > 21:
        print("Computer busted. You win.")
    elif user_score > 21:
        print("User busted, Computer wins")
    elif user_score == 21 and len(user_card) == 2:
        print("User has Blackjack! You win")
    elif computer_score == 21 and len(computer_card) == 2:
        print("Computer has Blackjack! You lose")
    elif user_score == 21:
        print("You win! You got 21")
    elif computer_score == 21:
        print("Computer wins! Computer got 21")
    elif computer_score == user_score:
        print("It's a draw")

should_continue = True
while should_continue == True:
    print(f"Your cards: {user_card}, current score {calculate_score(user_card)}")
    print(f"Computer's first card {computer_card[0]}")

    # 检查是否有Blackjack
    blackjack_occurred = False
    if user_score == 21 and len(user_card) == 2:
        print("User has the Blackjack. You win")
        should_continue = False
        blackjack_occurred = True
    elif computer_score == 21 and len(computer_card) == 2:
        print("Computer has the Blackjack. You lose")
        should_continue = False
        blackjack_occurred = True

    # 如果已经出现Blackjack,跳过后续逻辑
    if blackjack_occurred:
        continue

    # 检查用户是否爆牌或拿到21
    user_done = False
    if user_score > 21:
        if 11 in user_card:
            usr_indx_pos = user_card.index(11) 
            user_card[usr_indx_pos] = 1
            # 更新分数
            user_score = calculate_score(user_card)
            if user_score > 21:
                print("You got busted. You lose.")
                should_continue = False
                user_done = True
            elif user_score == 21:
                print("You have 21. You win")
                should_continue = False
                user_done = True
        else:
            print("You got busted. You lose.")
            should_continue = False
            user_done = True
    elif user_score == 21:
        print("You win. You got 21")
        should_continue = False
        user_done = True

    # 检查电脑状态
    computer_done = False
    if computer_score > 21:
        if 11 in computer_card:
            com_indx_pos = computer_card.index(11)
            computer_card[com_indx_pos] = 1
            # 更新分数
            computer_score = calculate_score(computer_card)
            if computer_score > 21:
                print("Computer busted. You win")
                should_continue = False
                computer_done = True
        else:
            print("Computer got busted. You win")
            should_continue = False
            computer_done = True
    elif computer_score == 21:
        print("Computer wins. Computer got 21")
        should_continue = False
        computer_done = True

    # 如果用户或电脑已经结束,跳过后续要牌逻辑
    if user_done or computer_done:
        continue

    # 询问是否要牌
    line_gap()
    if want_another_card() == "y":
        user_card.append(deal_card())
        user_score = calculate_score(user_card)
    else:
        final_score_calculation()
        should_continue = False

内容的提问来源于stack exchange,提问作者Prince Halder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 20:35:17