python:将多个参数从一个函数传递给另一个函数 [英] python: pass multiple arguments from one function to another
问题描述
我正在尝试学习python(以VBA为背景),目的是购买一个二十一点游戏作为教学练习.
I'm trying to learn python (with my VBA background) buy building a black-jack game as a pedagogical exercise.
我已经做了一些有关传递多个参数的搜索,但是我真的不明白我在解释中的发现.
I've done some searches about passing multiple arguments but I really don't understand what i'm finding in the way of explanations.
查看最后一个称为"hand"的函数,我试图利用从上一个函数作为"return"传递的三个单独的值.
Looking at the last function called 'hand' i'm trying to make use of three separate values passed as a 'return' from a previous function.
我收到以下错误:
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)
我做错了什么?如何提高效率?对于解决方案或阅读材料的任何建议,将不胜感激.
What am I doing wrong? How can I be more efficient? Any suggestions on solutions or readings are much appreciated.
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())))
推荐答案
尝试打印手(* deal(shuffle(load_deck())))
*
告诉python做参数开箱.
The *
tells python to do argument unpacking.
这篇关于python:将多个参数从一个函数传递给另一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!