i want to print all cards in the deck (randomly). The program runs and prints up to 48 cards (prints different amount every time program is executed). i suspect my problem lies within the get_card() function. this is my first program so please be nice (=
import random
class Deck(object):
def __init__(self,deck={},suit=[],suitDict={},cardValue=0,cardKey={}):
self.deck=deck
self.suit=suit
self.suitDict=suitDict
self.cardValue=cardValue
self.cardKey=cardKey
def create_deck(self):
spades={'Ace':[1,10,11],2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,10:10,'Jack':10,'Queen':10,'King':10}
hearts={'Ace':[1,10,11],2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,10:10,'Jack':10,'Queen':10,'King':10}
diamonds={'Ace':[1,10,11],2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,10:10,'Jack':10,'Queen':10,'King':10}
clubs={'Ace':[1,10,11],2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,10:10,'Jack':10,'Queen':10,'King':10}
self.deck={'Spades':spades,'Clubs':clubs,'Hearts':hearts,'Diamonds':diamonds}
print 'Deck Created'
def get_card(self):
while 1:
#gets random suit
self.suit=random.sample(self.deck,1)
self.suitDict=self.deck[self.suit[0]]
if self.suit[0] in self.deck:
#get random key[CARD]
self.cardKey=random.sample(self.suitDict,1)
if self.cardKey[0] in self.suitDict:
#get card value
self.cardValue=self.deck[self.suit[0]].pop(self.cardKey[0])
break
else:
self.get_card()
else:
self.get_card()
def return_hand(self):
self.get_card()
return [self.suit,self.cardKey,self.cardValue]
d=Deck()
d.create_deck()
x = 52
while x!=0:
print d.return_hand()
x-=1
Well this answer just fixes the specific problem you're facing which is not getting all the cards from your deck. I made the minimum required changes to just make your program work. You should how ever try to make the design and code cleaner.
This is the code:
import random
class Deck(object):
def __init__(self,deck={},suit=[],suitDict={},cardValue=0,cardKey={}):
self.deck=deck
self.suit=suit
self.suitDict=suitDict
self.cardValue=cardValue
self.cardKey=cardKey
def create_deck(self):
spades={'Ace':[1,10,11],2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,10:10,'Jack':10,'Queen':10,'King':10}
hearts={'Ace':[1,10,11],2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,10:10,'Jack':10,'Queen':10,'King':10}
diamonds={'Ace':[1,10,11],2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,10:10,'Jack':10,'Queen':10,'King':10}
clubs={'Ace':[1,10,11],2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,10:10,'Jack':10,'Queen':10,'King':10}
self.deck={'Spades':spades,'Clubs':clubs,'Hearts':hearts,'Diamonds':diamonds}
print 'Deck Created'
def empty(self):
return all(len(suit) == 0 for suit in self.deck.values())
def get_card(self):
while 1:
if self.empty():
break
#gets random suit
self.suit=random.sample(self.deck,1)
self.suitDict=self.deck[self.suit[0]]
if self.suitDict and self.suit[0] in self.deck:
#get random key[CARD]
self.cardKey=random.sample(self.suitDict,1)
if self.cardKey[0] in self.suitDict:
#get card value
self.cardValue=self.deck[self.suit[0]].pop(self.cardKey[0])
break
else:
self.get_card()
else:
self.get_card()
def return_hand(self):
self.get_card()
return [self.suit,self.cardKey,self.cardValue]
d=Deck()
d.create_deck()
x = 52
while x!=0:
print d.return_hand()
x-=1
I suggest you run a diff between this and your code to see exactly what has changed.
Basically:
self.suitDict
is not empty before trying to randomly choose a chard from it. i figured it out [=
import random
class Deck(object):
def __init__(self,deck={}):
self.deck=deck
def create_deck(self):
spades={'Ace':[1,10,11],2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,10:10,'Jack':10,'Queen':10,'King':10}
hearts={'Ace':[1,10,11],2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,10:10,'Jack':10,'Queen':10,'King':10}
diamonds={'Ace':[1,10,11],2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,10:10,'Jack':10,'Queen':10,'King':10}
clubs={'Ace':[1,10,11],2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,10:10,'Jack':10,'Queen':10,'King':10}
self.deck{'Spades':spades,'Clubs':clubs,'Hearts':hearts,'Diamonds':diamonds}
def get_card(self):
while True:
#picks random suit from deck // picks random key from deck dict.
#returns card attr. in a tuple
suit=random.choice(self.deck.keys())
if len(self.deck[suit]) > 0:
#picks random card from suit
card=random.sample(self.deck[suit],1)
cardValue=self.deck[suit].pop(card[0])
return (suit,card,cardValue)
else:
#deletes empty suit from deck.
del self.deck[suit]
and then this will print out all cards randomly:
deck=Deck()
deck.create_deck()
for i in range(52):
print i,deck.get_card()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.