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())))
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.
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.
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.
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?
try print hand(*deal(shuffle(load_deck())))
The *
tells python to do argument unpacking.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With