How do i print a hand of cards? I tried this and this seems to be returning the last card of the deck | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do i print a hand of cards? I tried this and this seems to be returning the last card of the deck

import random class Card: def __init__(self, suit= 0, rank= 2): self.suit = suit self.rank = rank suit_names = ['Clubs', 'Diamonds', 'Hearts', 'Spades'] rank_names = [None, 'Ace',2,3,4,5,6,7,8,9,10,'Jack', 'Queen', 'King'] def __str__(self): return '%s of %s' % (Card.rank_names[self.rank], Card.suit_names[self.suit]) #alternate code for comparing cards # def __cmp__(self, other): # if self.suit > other.suit: # return 1 # if self.suit < other.suit: # return -1 # if self.rank > other.rank: # return 1 # if self.rank < other.rank: # return -1 # return 0 def __cmp__(self, other): t1 = self.suit, self.rank t2 = other.suit, other.rank return cmp(t1, t2) class Deck: def __init__(self): self.cards = [] for suit in range(4): for rank in range(1,14): card = Card(suit, rank) self.cards.append(card) def __str__(self): res =[] for card in self.cards: res.append(str(card)) return '\n'.join(res) def pop_card(self): return self.cards.pop() def add_card(self, card): return self.cards.append(card) def shuffle(self): random.shuffle(self.cards) def sort(self): self.cards.sort(cmp= Card.__cmp__) return None def is_empty(self): return len(self.cards) == 0 def deal(self, hands, num= 999): Deck.__init__(self) self.hands = hands self.num = len(self.hands) for i in range(self.num): if self.is_empty():break card = self.pop_card() hand = hands[i % num] hand.add_card(card) class Hand(Deck): def __init__(self, label = ''): self.cards = [] self.label = label def move_cards(self,

26th Jan 2017, 12:57 PM
Tichaona Mtasa
Tichaona Mtasa - avatar
1 Answer
+ 5
Part of the code is missing. It is not possible to help you without the other part.
15th Mar 2017, 10:39 PM
Ulisses Cruz
Ulisses Cruz - avatar