当我运行我的文件时,我遇到了只有一张卡片打印的问题。它只打印“黑桃二”。我想打印整个甲板。当我不使用类时,我没有打印问题。但是现在我正在尝试理解类。谁能告诉我我做错了什么?我的代码如下.....
RANKS = {2: "Two", 3: "Three", 4: "Four", 5: "Five", 6: "Six", 7: "Seven",
8: "Eight", 9: "Nine", 10: "Ten", 11: "Jack", 12: "Queen",
13: "King", 14: "Ace"}
SUITS = {1: "Spades", 2: "Clubs", 3: "Diamonds", 4: "Hearts"}
################################################
class Card():
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
def __str__(self):
return "%s of %s" %(self.rank, self.suit)
class Deck():
def __init__(self):
self.cards = []
for r in RANKS.values():
for s in SUITS.values():
self.cards.append(Card(r, s))
def __str__(self):
for i in self.cards:
return str(i)
deck = Deck()
print(deck)
回首忆惘然
慕运维8079593
相关分类