Practise the using of the class
##I’am happy so much,today!
#Playing Cards
#演示对象的种组合
class Card:
“”“A Playing Cards”""
RANKS=[“A”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“10”,
“J”,“Q”,“K”]
SUITS=[“c”,“d”,“h”,“s”]
def __init__(self,rank,suit):
self.rank=rank
self.suit=suit
def __str__(self):
rep=self.rank+self.suit
return rep
#表示Card对象的集合
class Hand:
“”“A hand of playing cards”""
def init(self):
self.cards=[]
def __str__(self):
if self.cards:
rep = ""
for card in self.cards:
rep+=str(card)+" "
else:
rep="<empty>"
return rep
def clear(self):
self.cards=[]
def add(self,card):
self.cards.append(card)
def give(self,card,other_hand):
self.cards.remove(card)
other_hand.add(card)
#程序主体
card_01=Card(“A”,“c”)
print(“Show a Card object:”,card_01)
card_02=Card(“2”,“d”)
card_03=Card(“3”,“d”)
card_04=Card(“4”,“s”)
card_05=Card(“7”,“h”)
card_06=Card(“8”,“h”)
print(“Show the rest of the objects individually:”,
card_02,card_03,card_04,card_05,card_06,sep="|")
my_hand=Hand()
print(“Show my hand before I add cards:”,my_hand)
my_hand.add(card_01)
my_hand.add(card_02)
my_hand.add(card_03)
my_hand.add(card_04)
my_hand.add(card_05)
my_hand.add(card_06)
print(“Show my hand after adding 6 cards:”,my_hand)
your_hand=Hand()
print(“Show your hand before you add cards:”,your_hand)
my_hand.give(card_01,your_hand)
my_hand.give(card_03,your_hand)
print(“Gave the first two cards from my hand to your hand. Now your hand:”,your_hand)
print(“Now my hand:”,my_hand)
my_hand.clear()
print(“Now my hand have:”,my_hand,“after clearing .”)
your_hand.clear()
print(“Now ,after clearing ,your hand have:”,your_hand)