python - 在 Python 的 Class 中调用存储的列表参数
问题描述
在尝试为 Python 创建二十一点游戏时,我发现自己试图打印我的玩家手,但发现调用参数不会调用列表。最终检查解决方案让我找到了答案,但这只会让我更加困惑。
suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')
ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')
values = {'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 'Eight':8,
'Nine':9, 'Ten':10, 'Jack':10, 'Queen':10, 'King':10, 'Ace':11}class Card:
def __init__(self,suit,rank):
self.suit = suit
self.rank = rank
def __str__(self):
return self.rank + ' of ' + self.suit
class Deck:
def __init__(self):
self.deck = [] # start with an empty list
for suit in suits:
for rank in ranks:
self.deck.append(Card(suit,rank))
def __str__(self):
deck_comp = '' # start with an empty string
for card in self.deck:
deck_comp += '\n '+card.__str__() # add each Card object's print string
return 'The deck has:' + deck_comp
def shuffle(self):
random.shuffle(self.deck)
def deal(self):
single_card = self.deck.pop()
return single_card
class Hand:
def __init__(self):
self.cards = [] # start with an empty list as we did in the Deck class
self.value = 0 # start with zero value
self.aces = 0 # add an attribute to keep track of aces
def add_card(self,card):
self.cards.append(card)
self.value += values[card.rank]
使用此代码,我可以成功确定玩家手牌的价值:
test_player=Hand()
test_deck=Deck()
test_player.add_card(test_deck.deal())
test_player.add_card(test_deck.deal())
test_player.value
但是,在相同的逻辑中,我无法确定 self.cards 列表的值,因为以下提示错误:
test_player.cards
#or
print(test_player.cards)
更糟糕的是,我的解决方案指南中为此提供了以下功能:
def show(player):
print("\nPlayer's Hand:", *player.cards, sep='\n ')
show(test_player)
我希望有人可以澄清以下内容:
1)我不明白星号在*player.cards
做什么,但没有它这个功能将无法工作
2) 考虑到这个函数只是接收test_player
然后运行到Hand Class,这和test_player.cards 不是一回事吗?(哪个不起作用?)
解决方案
class Deck:
suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')
ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten',
'Jack', 'Queen', 'King', 'Ace')
values = {'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 'Eight':8,
'Nine':9, 'Ten':10, 'Jack':10, 'Queen':10, 'King':10, 'Ace':11}
def __init__(self):
self.deck = [] # start with an empty list
for suit in self.suits:
for rank in self.ranks:
self.deck.append(Card(suit,rank))
def __str__(self):
deck_comp = '' # start with an empty string
for card in self.deck:
deck_comp += '\n '+card.__str__() # add each Card object's print string
return 'The deck has:' + deck_comp
def shuffle(self):
random.shuffle(self.deck)
def deal(self):
single_card = self.deck.pop()
return single_card
问题是在类中没有调用卡片,使用此字典的一种方法是在类中,如果将其定义为常量,则在使用 self 方法调用之前,形成示例,self.suits 调用 suits 对象。以此证明
推荐阅读
- mysql - 清空 mysql 长文本列与缩小它
- python-3.x - 如果使用 python 不存在,则仅附加新行或更新以前的记录
- recursion - 递归期间在Scheme中创建封闭过程对性能的影响
- php - 我如何在 php 中使用 set 和 get 的静态私有属性
- python - AttributeError:类型对象“AuthorDetailView”没有属性“as_views”
- javascript - Lodash uniqBy created date?
- nginx - phpinfo 只在 nginx 上显示一个空白页面
- bash - bash中函数的多个名称/别名
- angular - 单元测试角度如果条件使按钮禁用
- c# - 当我添加分数时, scoreText 只是快速显示添加到自身的分数