Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: pass multiple arguments from one function to another

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())))
like image 739
DBWeinstein Avatar asked Aug 05 '12 02:08

DBWeinstein


People also ask

How do I Pass on all arguments in Python?

The standard way to pass on all arguments is as @JohnColeman suggested in a comment: As parameters, *args receives a tuple of the non-keyword (positional) arguments, and **kwargs is a dictionary of the keyword arguments.

What is an actual argument in Python?

An actual argument is an argument, which is present in the function call. We can pass multiple arguments to a python function by predetermining the formal parameters in the function definition.

How to pass data to a function in Python?

You can pass data objects like strings, lists, dictionaries, or set as an input parameter to the function, and in the function, we can perform operations on its and return as an output result. We can arguments inside the function calling parenthesis. The argument values will be comma-separated.

Can a function be passed as an argument to another function?

All functions in Python can be passed as an argument to another function (that just happens to be the sole purpose of lambda functions). A common example: key functions Besides the built-in filter function, where will you ever see a function passed into another function?


1 Answers

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

The * tells python to do argument unpacking.

like image 116
Jesse Aldridge Avatar answered Sep 21 '22 16:09

Jesse Aldridge