python:将多个参数从一个函数传递到另一个函数

2023-11-27

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

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

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

我收到以下错误:

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())))

try print hand(*deal(shuffle(load_deck())))

The *告诉 python 做什么论证解包.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python:将多个参数从一个函数传递到另一个函数 的相关文章

随机推荐