7

我正在尝试学习 python(具有我的 VBA 背景)购买构建二十一点游戏作为教学练习。

我已经进行了一些关于传递多个参数的搜索,但我真的不明白我在解释的方式中发现了什么。

查看最后一个名为“hand”的函数,我试图利用三个单独的值作为前一个函数的“return”传递。

我收到以下错误:

Traceback (most recent call last):
File "decky15.py", line 56, in <module>
print hand(deal(shuffle(load_deck())))
TypeError: hand() takes exactly 3 arguments (1 given)

我究竟做错了什么?我怎样才能更有效率?非常感谢任何有关解决方案或读数的建议。

import random


def load_deck():
    suite = ('Spades', 'Hearts', 'Diamonds', 'Clubs')
    rank = ('2', '3', '4', '5', '6', '7', '8', '9', '10', "Jack", "Queen", "King", "Ace")
    full_deck = {}
    i = 0
    for s in suite:
        for r in rank:
            full_deck[i] = "%s of %s" % (r, s)
            i += 1
    return full_deck

def pick_item(dict_in_question):   
    card_key = random.choice(dict_in_question.keys()) 
    card = dict_in_question[card_key]  
    del dict_in_question[card_key]  
    return card

def shuffle(dict_in_question):  #takes a dictionary as an argument and shuffles it
    shuff_dict = {}
    n = len(dict_in_question.keys())
    for i in range(0, n):
        shuff_dict[i] = pick_item(dict_in_question)
    return shuff_dict

def deal(dict_in_question):
dealer ={}
player = {}
for i in range (2):
    player[i] = pick_item(dict_in_question)
        dealer[i] = pick_item(dict_in_question)
return (player, dealer, dict_in_question)

def hand(player, dealer, dict_in_question):
print"Here are the opening hands:"
print"Dealer: %s" % dealer(1)
print" - " * 10
print"Your hand:"
print"%s" % player[0]
print"%s" % player[1]
return 0

print hand(deal(shuffle(load_deck())))  #changed to: hand(*deal(shuffle(load_deck())))
4

1 回答 1

13

尝试print hand(*deal(shuffle(load_deck())))

*告诉 python 做参数解包。

于 2012-08-05T02:38:13.820 回答