python: pass multiple arguments from one function to another

≯℡__Kan透↙ 提交于 2021-02-04 15:17:34

问题


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.

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.

I get the following error:

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

回答1:


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

The * tells python to do argument unpacking.



来源:https://stackoverflow.com/questions/11813548/python-pass-multiple-arguments-from-one-function-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!