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())))
8
  • @klobucar now I get "none" returned. How can I just get the prints? Commented Aug 5, 2012 at 2:48
  • @klobucar: "python functions return true by default". No, they return None by default. Commented Aug 5, 2012 at 2:49
  • how do I get rid of the None? Commented Aug 5, 2012 at 2:50
  • If you don't want to print the return value from hand(), simply call hand(deal(shuffle(load_deck()))) without a print statement. Commented Aug 5, 2012 at 2:51
  • Err, I worded that poorly. I meant lose the "return 0", because python functions return something all the time and lose the print at the end. Commented Aug 5, 2012 at 2:57

1 Answer 1

13

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

The * tells python to do argument unpacking.

Sign up to request clarification or add additional context in comments.

3 Comments

By the way, is there a better way to call the fuctions? i.e. without having 3 nested functions?
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...
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 !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.