Practise the using of the class

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值