selin_0 2021-06-19 21:56 采纳率: 33.3%
浏览 42

紧急求助!!用初级python function解决21点(黑杰克)问题

菜鸟初学者求助,以下能做几个stage做几个,最终要求的输出结果示例在最后。很紧急明早要交!拜托拜托!!!应该不是太难,只是我真的太菜了www  (可追加酬金)


 已给的随机洗牌代码:

#
# playing_cards module - PSP Assignment 3, sp3, 2021.
# DO NOT MODIFY!
#

import random


# Deck of cards - first letter represents the face value and
# second letter represents the suit
deck = ['AH','2H','3H','4H','5H','6H','7H','8H','9H','TH','JH','QH','KH',
        'AD','2D','3D','4D','5D','6D','7D','8D','9D','TD','JD','QD','KD',
        'AS','2S','3S','4S','5S','6S','7S','8S','9S','TS','JS','QS','KS',
        'AC','2C','3C','4C','5C','6C','7C','8C','9C','TC','JC','QC','KC']

# Playing deck in use
playing_deck = []


# Function to determine whether there are any cards left in the
# deck of playing cards
# Parameters: No parameters
# Returns: True if the deck is empty, False otherwise
def is_empty_deck():

    # Check to see whether playing deck is empty
    return len(playing_deck) == 0


# Function to rebuild and shuffle the deck
# Parameters: No parameters
# Returns: Nothing is returned from the function.
def reset_deck():
    global playing_deck

    # Create new playing deck
    playing_deck = deck.copy()
    
    # Shuffle deck
    random.shuffle(playing_deck)


# Function to deal one card
# Parameters: No parameters
# Returns: A string (containing two characters) representing
# the card delt, i.e. '2H' meaning 2 of Hearts
def deal_one_card():
    
    # Check to see whether there are any cards left
    if is_empty_deck():
    
        # Rebuild and shuffle deck
        reset_deck()

    # Return a card (string of two characters)
    return playing_deck.pop(0)

题目:



 

 

最终输出结果:

  • 写回答

1条回答 默认 最新

  • lol_hw 2023-01-18 18:53
    关注
    
    import random
    
    # Create a list of card values
    card_values = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]
    
    # Create a function to calculate the total point value of a hand
    def calculate_points(hand):
        point_value = sum(hand)
        for card in hand:
            if card == 11 and point_value > 21:
                point_value -= 10
        return point_value
    
    # Create a function to deal a new card
    def deal_card():
        return random.choice(card_values)
    
    # Initialize the player's hand and the dealer's hand
    player_hand = [deal_card(), deal_card()]
    dealer_hand = [deal_card(), deal_card()]
    
    # Check for blackjack
    if calculate_points(player_hand) == 21:
        print("Blackjack! You win.")
        exit()
    
    # Ask the player if they want to hit or stand
    while True:
        choice = input("Do you want to hit or stand? ")
        if choice == "hit":
            player_hand.append(deal_card())
            print("Player's hand:", player_hand)
            print("Player's point value:", calculate_points(player_hand))
            if calculate_points(player_hand) > 21:
                print("You bust! You lose.")
                exit()
        elif choice == "stand":
            break
        else:
            print("Invalid choice.")
    
    # The dealer's turn
    while calculate_points(dealer_hand) < 17:
        dealer_hand.append(deal_card())
        print("Dealer's hand:", dealer_hand)
        print("Dealer's point value:", calculate_points(dealer_hand))
        if calculate_points(dealer_hand) > 21:
            print("Dealer busts! You win.")
            exit()
    
    # Compare the point values of the player's hand and the dealer's hand
    if calculate_points(player_hand) > calculate_points(dealer_hand):
        print("You win!")
    elif calculate_points(player_hand) == calculate_points(dealer_hand):
        print("It's a push.")
    else:
        print("You lose.")
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序