我目前正在一個二十一點遊戲上工作,而且由於我目前有它的結構,我有一個Hand類是一個Card對象列表,我試圖引用一個特定的卡在手中。'手'對象不支持索引
def get_move(self):
if self.balance > self.bet and not self.isSplit:
if self.hand[0].point == self.hand[1].point:
問題出現在第三行上。我收到以下錯誤:
Traceback (most recent call last):
File "driver.py", line 126, in <module>
player.get_move()
File "/home/ubuntu/workspace/finalproject/human_player.py", line 29, in get_move
if self.hand[0].point == self.hand[1].point:
TypeError: 'Hand' object does not support indexing
爲什麼它不讓我的索引通過手?這裏是構造我的手類:
class Hand:
def __init__(self):
self.hand = []
編輯:我做我的主要方法爲每一個玩家手對象:
# creating the dealer for the game
dealer_hand = hand.Hand()
dan_the_dealer = dealer.Dealer(dealer_hand)
# for however many players you start with, add each new player to an array of players
for i in range(num_players):
player_name = input("\nWhat is Player %i's name?\n" % (i+1))
new_hand = hand.Hand()
new_player = human_player.HumanPlayer(player_name, starting_balance, 0, new_hand)
players.append(new_player)
你不會爲你手中的'Hand'對象重新定義'hand'嗎? – Iluvatar
該錯誤消息表明'self.hand'是一個'Hand'對象而不是一個列表。你所顯示的代碼中沒有任何內容會導致這種情況發生。因此 - 問題在於你沒有顯示的代碼。由於您沒有顯示包含錯誤的代碼,因此沒有任何人可以幫助。 –
我不這麼認爲。如果我要尋找這樣的跡象,那會是什麼樣子? – macschmidt33