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

标签 python function argument-passing

我正在尝试学习 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())))

最佳答案

尝试 打印手牌(*交易(洗牌(load_deck())))

* 告诉 python 做 argument unpacking .

关于python:将多个参数从一个函数传递到另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11813548/

相关文章:

python - 从文本文件创建字典 2 个值

使用下拉列表运行的 Javascript 函数

c++ - 从 'const char*' 到 'char' 的无效转换

python - 使用 Flask (Python) 在谷歌数据存储中存储图像

python - Tensorflow - 对多个图像进行批量预测

python - 在 __init__.py 中定义任何变量

javascript - 在类内和类外定义 JavaScript 函数

python - numpy.save() 内的 Pickle TypeError

Python:装饰简单的递归函数

c# 内联函数参数和临时变量