7

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())))
DBWeinstein
  • 8,605
  • 31
  • 73
  • 118

1 Answers1

13

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

The * tells python to do argument unpacking.

Jesse Aldridge
  • 7,991
  • 9
  • 48
  • 75
  • By the way, is there a better way to call the fuctions? i.e. without having 3 nested functions? – DBWeinstein Aug 05 '12 at 02:52
  • Seems fine to me -- it's functional style. I guess you could merge the functions so you only have one call; or use some variables to store the returned results... – Jesse Aldridge Aug 05 '12 at 02:56
  • Wow just learned why you put the `*` in `*args` declaration. I was passing the `args` from one function to the next without the `*` and was getting a tuple with one element containing the whole `args` tuple. Thanks ! – Yohan Obadia Mar 01 '18 at 16:41