Reputation: 9509
I'm learning python (with VBA background) by building a black-jack game (Yes, I've asked a bunch of questions using blackjack as an example).
Here's my code:
import random
class DECK():
def load_deck(self):
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(self, deck):
card_key = random.choice(deck.keys())
new_card = deck[card_key]
del deck[card_key]
return (deck, new_card)
def missing_card(self, deck):
temp_deck = DECK()
print temp_deck
d1 = DECK()
deck1 = d1.load_deck()
deck1, card1 = d1.pick_item(deck1)
print card1
d1.missing_card(d1)
Here's what I get in the terminal (file namehand_c.py
):
$ python hand_c.py
Ace of Clubs
<__main__.DECK instance at 0x10bb0d248>
$
Why does one function work pick_item
, but not the other missing_card
?
Per the first answer, I changed the function definition to:
def missing_card(self, deck):
deckC1 = DECK()
temp_deck = deckC1.load_deck
print temp_deck
But now I get the following from the terminal:
$ python hand_c.py
Jack of Diamonds
<bound method DECK.load_deck of <__main__.DECK instance at 0x10500e248>>
$
Upvotes: 1
Views: 9623
Reputation: 16403
I modified your program to work. I introduced a constructor and changed the representation of your deck to a list instead of a dictionary, and made it to a instance variable that belongs to the deck you create in the line d1 = DECK()
. Every method in your class now has access to your deck, without revealing your internal representation of the deck to the world and you only have to work with the one DECK object.
import random
class DECK():
def __init__(self):
suite = ('Spades', 'Hearts', 'Diamonds', 'Clubs')
rank = ('2', '3', '4', '5', '6', '7', '8', '9', '10', "Jack", "Queen", "King", "Ace")
self.full_deck = []
for s in suite:
for r in rank:
self.full_deck.append("%s of %s" % (r, s))
def pick_item(self):
card_key = random.randint(0, len(self.full_deck)-1)
new_card = self.full_deck[card_key]
del self.full_deck[card_key]
return new_card
def missing_card(self):
print self.full_deck
d1 = DECK()
card1 = d1.pick_item()
print card1
d1.missing_card()
Upvotes: 2
Reputation: 251428
You should go through the Python tutorial. Your code has many problems. Most basically, you are using the class just as a bag to hold functions, without actually holding the deck data as part of the class instance. That is, you return the deck as a dictionary, then pass it back in to another function. It would be better to store the deck in an attribute (e.g., self.deck
) and then have other functions use that.
Anyway, the reason it doesn't print the dictionary is quite simple. You do this:
temp_deck = DECK()
print temp_deck
So you create a variable temp_deck
and set it equal to a new instance of class DECK. Then you print it. Well, of course it won't print a dictionary. temp_deck
is not a dictionary. It's a DECK object. If you want the dictionary with your current code, you would need to do temp_deck.load_deck()
just like you did with your original deck, and then print the result of that.
Upvotes: 1