首页 > 解决方案 > 将一个函数的值分配给另一个函数

问题描述

我希望 get_suit(hand) 函数将正确的手分配给 display_hand 函数中的打印函数。它所做的只是为手中的两张牌分配一套花色。

输出应为: Player's hand is 19 :[('Q', 'of', 'Diamonds'), ('8', 'of', 'Spades')

我认为问题可能出在 get_suits 函数中。

hand = ['QD', '8S']

def get_hand_total(hand):
    total = 0
    for card in hand:
        
        card_value = card[0] 
       
            
        if card_value in 'KQJT':
            total += 10
        elif card_value == 'A':
            total += 11
        else:
            total += int(card_value)
                      
    return total

def get_suits(hand):    
    suit = ''
    for card in hand:
        
        card_suit = card[1]
        if card_suit == 'S':
            suit = 'Spades'
        elif card_suit == 'C':
            suit = 'Clubs'
        elif card_suit == 'D':
            suit = 'Diamonds'
        else:
            suit = 'Hearts'
    
    return suit

def display_hand(hand_text, hand):
    final_hand = []
    
    
    for card in (hand):
        get_suits(hand)
        get_hand_total(hand)
        hand_list = card[0], 'of', get_suits(hand)
        final_hand.append(hand_list)
        
    print(hand_text, get_hand_total(hand), ':', final_hand)

标签: functionloopsfor-loop

解决方案


推荐阅读