【发布时间】:2020-10-06 09:07:46
【问题描述】:
所以我目前的二十一点项目有问题。我目前正在尝试创建一个班级甲板(下面的代码)
import random
class Cards:
def __init__(self, suit, number):
self.suit = suit
self.number = number
def display_card(self):
print("{} of {}".format(self.number,self.suit))
def card_syntax(self):
if self.number == 1:
self.number = "Ace"
if self.number == 11:
self.number = "Jack"
if self.number == 12:
self.number = "Queen"
if self.number == 13:
self.number = "King"
print("{} of {}".format(self.number, self.suit))
class Deck:
def __init__(self):
self.cards = []
self.card_characterists()
def card_characterists(self):
for suit in ["Hearts", "Spades", "Clubs", "Diamonds"]:
for number in range(1,14):
self.cards.append(Cards(suit,number))
def shuffle_cards(self):
random.shuffle(self.cards)
def draw(self):
return self.cards.pop()
def show(self):
for c in self.cards:
print(c)
我遇到的问题与 Deck 类中的 show() 方法有关。我尝试使用 FOR 循环来获取对象。
def show(self):
for c in self.cards:
print(c)
我似乎得到了以下结果:
<__main__.Cards object at 0x7f9269ed6390>
<__main__.Cards object at 0x7f9269ed6198>
<__main__.Cards object at 0x7f9269ed6358>
<__main__.Cards object at 0x7f9269ed6b00>
<__main__.Cards object at 0x7f9269ed6588>
<__main__.Cards object at 0x7f9269ed6710>
<__main__.Cards object at 0x7f9269ed6a58>
<__main__.Cards object at 0x7f9269ed6a20>
<__main__.Cards object at 0x7f9269ed68d0>
<__main__.Cards object at 0x7f9269ed62e8>
<__main__.Cards object at 0x7f9269ed6320>
<__main__.Cards object at 0x7f9269ed69e8>
<__main__.Cards object at 0x7f9269ed61d0>
<__main__.Cards object at 0x7f9269ed6978>
<__main__.Cards object at 0x7f9269ed60b8>
<__main__.Cards object at 0x7f9269ed6470>
<__main__.Cards object at 0x7f9269ed6ba8>
<__main__.Cards object at 0x7f9269ed6748>
<__main__.Cards object at 0x7f9269ed62b0>
<__main__.Cards object at 0x7f9269ed6828>
<__main__.Cards object at 0x7f9269ed6be0>
<__main__.Cards object at 0x7f9269ed6780>
<__main__.Cards object at 0x7f9269ed65f8>
<__main__.Cards object at 0x7f9269ed6ac8>
<__main__.Cards object at 0x7f9269ed6400>
<__main__.Cards object at 0x7f9269ed6668>
<__main__.Cards object at 0x7f9269ed64e0>
<__main__.Cards object at 0x7f9269ed64a8>
<__main__.Cards object at 0x7f9269ed69b0>
<__main__.Cards object at 0x7f9269ed66d8>
<__main__.Cards object at 0x7f9269ed6c18>
<__main__.Cards object at 0x7f9269ed6898>
<__main__.Cards object at 0x7f9269ed6630>
<__main__.Cards object at 0x7f9269ecbfd0>
<__main__.Cards object at 0x7f9269ed67b8>
<__main__.Cards object at 0x7f9269ed65c0>
<__main__.Cards object at 0x7f9269ed66a0>
<__main__.Cards object at 0x7f9269ed6860>
<__main__.Cards object at 0x7f9269ed6908>
<__main__.Cards object at 0x7f9269ed6550>
<__main__.Cards object at 0x7f9269eb64e0>
<__main__.Cards object at 0x7f9269ed6a90>
<__main__.Cards object at 0x7f9269ed63c8>
<__main__.Cards object at 0x7f9269ed6940>
<__main__.Cards object at 0x7f9269ecb710>
<__main__.Cards object at 0x7f9269ed6518>
<__main__.Cards object at 0x7f9269ed67f0>
<__main__.Cards object at 0x7f9269ed6080>
<__main__.Cards object at 0x7f9269ed60f0>
<__main__.Cards object at 0x7f9269ed6b38>
<__main__.Cards object at 0x7f9269ed6438>
有没有办法从 self.cards() 中获得可读的结果。这是我的第一篇文章,所以我希望您不要介意这句话的措辞不当。
【问题讨论】:
-
如果这是关于python的问题,那么添加python标签会很有帮助。如果是 python,这可能会有所帮助:stackoverflow.com/questions/3204614/…。 (特别是
def __str__(self):等) -
是的,在你的类中定义一个
__str__方法并返回一个你喜欢的字符串。
标签: python class for-loop oop object