在 python 中实现一副纸牌。调用 list.pop() 时尝试打印所有卡片时出现问题
Implement a Deck of cards in python. Problem when trying to print all cards while calling list.pop()
class Card:
def __init__(self, suit, value):
self.suit = suit
self.value = value
def show(self):
# print(f"{self.value} of {self.suit}")
return (self.suit, self.value)
class Deck(Card):
"""
Deck is collection of 52 cards.
"""
colour = ('Hearts', 'Diamonds', 'Spades', 'Clubs')
rank = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')
def __init__(self):
d = []
for s in Deck.colour:
for r in Deck.rank:
c = Card(s,r)
d.append(c)
self.pack = d
def draw(self):
return self.pack.pop()
mydeck = Deck()
j =1
for i in mydeck.pack:
print(j, "\t", mydeck.draw().show(), " count remaining ", len(mydeck.pack))
j +=1
在尝试打印列表 mydeck.deck
的内容时,它只打印了总值的一半。如果再次 运行,它会打印下一半的值。
请帮我看看为什么所有内容都打印不出来?
我是初学者,非常感谢任何反馈。
提前致谢。
只是不要在循环内调用 draw() — i
已经遍历了 mydeck.pack 中的每张卡片,因此您可以只调用 i.show()
。您可能希望将 i
重命名为 card
。
正如 Ed Ward 在评论中指出的,您也肯定不希望 Deck 继承自 Card。
我是这样修复的:
class Card:
def __init__(self, suit, value):
self.suit = suit
self.value = value
def show(self):
# print(f"{self.value} of {self.suit}")
return (self.suit, self.value)
class Deck():
"""
Deck is collection of 52 cards.
"""
colour = ('Hearts', 'Diamonds', 'Spades', 'Clubs')
rank = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')
def __init__(self):
d = []
for s in Deck.colour:
for r in Deck.rank:
c = Card(s,r)
d.append(c)
self.pack = d
def draw(self):
return self.pack.pop()
mydeck = Deck()
j =1
breakpoint()
for i in mydeck.pack:
print(j, "\t", i.show(), " count remaining ", len(mydeck.pack)-j)
j +=1
输出
1 ('Hearts', 'Two') count remaining 51
2 ('Hearts', 'Three') count remaining 50
3 ('Hearts', 'Four') count remaining 49
4 ('Hearts', 'Five') count remaining 48
5 ('Hearts', 'Six') count remaining 47
6 ('Hearts', 'Seven') count remaining 46
7 ('Hearts', 'Eight') count remaining 45
8 ('Hearts', 'Nine') count remaining 44
9 ('Hearts', 'Ten') count remaining 43
10 ('Hearts', 'Jack') count remaining 42
......................
class Card:
def __init__(self, suit, value):
self.suit = suit
self.value = value
def show(self):
# print(f"{self.value} of {self.suit}")
return (self.suit, self.value)
class Deck(Card):
"""
Deck is collection of 52 cards.
"""
colour = ('Hearts', 'Diamonds', 'Spades', 'Clubs')
rank = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')
def __init__(self):
d = []
for s in Deck.colour:
for r in Deck.rank:
c = Card(s,r)
d.append(c)
self.pack = d
def draw(self):
return self.pack.pop()
mydeck = Deck()
j =1
for i in mydeck.pack:
print(j, "\t", mydeck.draw().show(), " count remaining ", len(mydeck.pack))
j +=1
在尝试打印列表 mydeck.deck
的内容时,它只打印了总值的一半。如果再次 运行,它会打印下一半的值。
请帮我看看为什么所有内容都打印不出来?
我是初学者,非常感谢任何反馈。 提前致谢。
只是不要在循环内调用 draw() — i
已经遍历了 mydeck.pack 中的每张卡片,因此您可以只调用 i.show()
。您可能希望将 i
重命名为 card
。
正如 Ed Ward 在评论中指出的,您也肯定不希望 Deck 继承自 Card。
我是这样修复的:
class Card:
def __init__(self, suit, value):
self.suit = suit
self.value = value
def show(self):
# print(f"{self.value} of {self.suit}")
return (self.suit, self.value)
class Deck():
"""
Deck is collection of 52 cards.
"""
colour = ('Hearts', 'Diamonds', 'Spades', 'Clubs')
rank = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')
def __init__(self):
d = []
for s in Deck.colour:
for r in Deck.rank:
c = Card(s,r)
d.append(c)
self.pack = d
def draw(self):
return self.pack.pop()
mydeck = Deck()
j =1
breakpoint()
for i in mydeck.pack:
print(j, "\t", i.show(), " count remaining ", len(mydeck.pack)-j)
j +=1
输出
1 ('Hearts', 'Two') count remaining 51
2 ('Hearts', 'Three') count remaining 50
3 ('Hearts', 'Four') count remaining 49
4 ('Hearts', 'Five') count remaining 48
5 ('Hearts', 'Six') count remaining 47
6 ('Hearts', 'Seven') count remaining 46
7 ('Hearts', 'Eight') count remaining 45
8 ('Hearts', 'Nine') count remaining 44
9 ('Hearts', 'Ten') count remaining 43
10 ('Hearts', 'Jack') count remaining 42
......................