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

如何实现Blackjack玩家手牌点数求和?现有Python代码问题求助

Fixing Your Blackjack Hand Total Calculation

Got it, let's tweak your existing Blackjack code to fix those hand total issues—no need to jump to dictionaries if you want to stick with your current setup! Here's how we'll address each problem step by step:

Key Problems We'll Fix

  • Wrong card value extraction: Your current code grabs only the first character, so '10 of Hearts' gets treated as '1' instead of 10.
  • Total not accumulating: You were overwriting the total variable each loop instead of adding to it.
  • Rigid Ace handling: Aces were fixed to 11, but we need to adjust them to 1 if the total goes over 21.

Step-by-Step Changes

1. Fix Card Value Extraction

Instead of using i[0] to get the rank, split each card string using ' of ' as the separator. This gives us the full rank (like '10' instead of just '1'):

rank = card.split(' of ')[0]

2. Correctly Calculate the Total

We'll add to the total instead of overwriting it, and handle face cards, aces, and numeric ranks properly:

  • Face cards (Jack/Queen/King) add 10
  • Start with Aces as 11 (we'll adjust later if needed)
  • Numeric ranks get converted to integers and added to the total

3. Adjust Aces for Busts

After calculating the initial total, check if it's over 21. If it is, subtract 10 for each Ace (turning 11 into 1) until the total is 21 or lower, or we run out of Aces to adjust.

Full Modified Code

import random

PlayersHand = []
DealersHand = []
Ranks = ["2", "3", "4", "5", "6", "7", "8", "9", "10","Ace", "Jack", "Queen", "King"]
Suits = ["Hearts", "Diamonds", "Clubs", "Spades"]
Deck = []

# Build the deck
for num in Ranks:
    for suit in Suits:
        card = num + ' of ' + suit
        Deck += [card]
random.shuffle(Deck)

# Get player name (added this since your original code uses 'name' but didn't define it)
name = input("Enter your name: ")

# Deal initial hands
PlayersHand = random.choices(Deck, k=2)
print(f"\n{name}, your cards are {PlayersHand}")
DealersHand = random.choices(Deck, k=1)
print(f"Dealer, your cards are Blank + {DealersHand}")

# Calculate initial player total
total = 0
ace_count = 0
for card in PlayersHand:
    rank = card.split(' of ')[0]  # Fix: Get full rank instead of first character
    print(f"Card: {card} | Rank: {rank}")
    print("-------------")
    
    if rank in ["Jack", "Queen", "King"]:  # Match full rank names from your Ranks list
        total += 10
    elif rank == "Ace":
        total += 11  # Start with Ace as 11
        ace_count += 1
    else:
        total += int(rank)  # Add numeric value as integer

# Adjust Aces if total is over 21
while total > 21 and ace_count > 0:
    total -= 10
    ace_count -= 1
    print(f"Adjusting Ace from 11 to 1. New total: {total}")

print(f"\nYour total hand value is: {total}")

# Function to add a new card and update total
def hit_me():
    global total, PlayersHand, ace_count
    new_card = random.choice(Deck)
    PlayersHand.append(new_card)
    print(f"\nYou drew: {new_card}")
    
    rank = new_card.split(' of ')[0]
    if rank in ["Jack", "Queen", "King"]:
        total += 10
    elif rank == "Ace":
        total += 11
        ace_count += 1
    else:
        total += int(rank)
    
    # Adjust Aces again if needed after hitting
    while total > 21 and ace_count > 0:
        total -= 10
        ace_count -= 1
        print(f"Adjusting Ace from 11 to 1. New total: {total}")
    
    print(f"Your updated total is: {total}")
    print(f"Your current hand: {PlayersHand}")

# Example usage: Uncomment to test hitting
# hit_me()

What's Changed?

  • Fixed rank extraction so '10' cards are correctly recognized
  • Now accumulates the total instead of overwriting it
  • Added dynamic Ace adjustment to prevent busting
  • Included a hit_me() function to add new cards and update the total automatically

内容的提问来源于stack exchange,提问作者P Hogan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 15:57:26