- I have 2 classes - Card and Hand
- Card is with attributes : suit, rank
- Hand is created by appending empty list by cards. So it`s attributes are cards.
I need to be written in console something like:
Ace D , Queen H
I found in Google that I need to right __str__(self)
method carefully.
I do not know how in method __str__(self)
write stuff that my output would be like I mentioned above. For example :
return self.hand
does the following :
[<__main__.Card object>, <__main__.Card object>]
I think that Hand object is a list of such kind :
[[card 1],[card 2],..]
which is
[[suit 1,rank 1],[..,..],..]
But I do not know how to take that information from list of list. Such write does not help: hand[i][0] for suit and hand[i][1] for rank of i-th card Still do not get . Here is code:
class Card:
def __init__(self, suit, rank):
if (suit in SUITS) and (rank in RANKS):
self.suit = suit
self.rank = rank
else:
print "Invalid card: ", suit, rank
def __str__(self):
return self.suit + self.rank
class Hand:
def __init__(self):
self.hand = []
def __str__(self):
return str(self.hand)
def add_card(self, card):
self.hand.append(card)
Creating OBJECT CARDS
c1 = Card("S", "A")
c2 = Card("C", "2")
Creating OBJECT HAND
test_hand = Hand()
Adding cards to hand
test_hand.add_card(c1)
test_hand.add_card(c2)
print test_hand
result is:
[<__main__.Card object>, <__main__.Card object>]