【发布时间】:2022-06-17 19:54:19
【问题描述】:
我已经尝试了大约一个星期来完成这项工作。我的目标是获取卡片列表并将列表项 0、1 和 2 附加到 user_hand 列表中。当我运行代码时,我会得到诸如 main.Card object at 0x0000021DA02924D0> 打印而不是 A♥。我做错了什么来产生这样的错误?这是我第一次使用 python 类,所以我可能会在那里绊倒。
import random
user_hand = []
class Card():
def __init__(self, name, suit):
self.name = name
self.suit = suit
def print_card(self):
suits = {"H":"♥","D":"♦","C":"♣","S":"♠"}
print(f"{self.name}{suits[self.suit]}")
class Carddeck():
def __init__(self):
self.cards = []
names = ("2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A")
suits = ("H", "D", "C", "S")
for suit in suits:
for name in names:
card = Card(name, suit)
self.cards.append(card)
def cupid(self):
random.shuffle(self.cards)
def dealing(self):
user_hand.append(self.cards[0])
user_hand.append(self.cards[1])
user_hand.append(self.cards[2])
card = Card("A", "H")
card.print_card()
deck = Carddeck() # creates the deck.
deck.cupid() # shuffles the deck
deck.dealing() # deals to player, but this gives the strange output
for card in deck.cards:
card.print_card()
print(user_hand)
【问题讨论】:
标签: python class append playing-cards