Python:从函数内部追加实例属性

时间:2013-01-11 17:32:42

标签: python class instance

我正在尝试编写一个基本的纸牌游戏,但尝试尽可能多地使用它来适用于未来的纸牌游戏。这是我的代码。

decks = [  ]
players = [  ]

class Player:
global players
def __init__( self, name ):
    self.hand = [  ]
    self.name = ''
    self.score = 0
    self.name = 'playerX'
    self.score = 0
    players.append( name )


class Deck:
    global decks
    global players

def __init__( self, name ):
    self.SUITS = [ 'C', 'D', 'H', 'S' ]
    self.RANKS = [ '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A' ]
    self.deck = [  ]
    for r in self.RANKS:
        for s in self.SUITS:
            self.deck.append( ( r, s ) )
    decks.append( name )

def shuffle( self ):
    shuffle( self.deck )

def display( self ):
    for e in range(0, len( self.deck ), 4):
        for i in self.deck[e:e+4]:
            print i,
        print

def dealXtoOne( number, deck, player ):
    ''' number of cards from a deck to named player'''
    if len( deck.deck ) >= number:
        for n in range( 0, number ):
            player.hand.append( deck.deck.pop(  ) )
            print 'Player1 hand: ', p1.hand
    else:
        return 'Insufficient cards in the deck.'

def dealXtoEach( number, deck ):
    ''' number of cards from a deck to each player'''
    global players
    print 'debug 0'
    if len( deck.deck ) >= number*len( players ):
        print 'debug 1'
        for cards in range( 0, number*len( players ) ):
            print 'debug 2'
            for player in players:
                print 'debug 3'
                print player
                print player.hand
                player.hand.append( deck.deck.pop(  ) )
                print player.hand
    else:
        return 'Insufficient cards in the deck.'



def value( card ):
    if card[ 0 ] == 'J': return 11
        elif card[ 0 ] == 'Q': return 12
        elif card[ 0 ] == 'K': return 13
        elif card[ 0 ] == 'A': return 14
        else: return card[ 0 ]

def topCard( deck ):
    return deck.deck[ -1 ]

def color( card ):
    if card[ 1 ] in [ 'Diamonds', 'Hearts' ]:
        return 'Red'
    else:
        return 'Black'


p1 = Player( 'p1' )
p2 = Player( 'p2' )
d = Deck( 'd' )

d.display(  )
print p1.hand
dealXtoOne( 3, d, p1 )
d.display(  )
print p1.hand
dealXtoEach( 3, d )
d.display(  )

我确定这很垃圾,这显然是一项正在进行的工作,但我正在自学编程。对不起,如果你的眼睛流血!

因此,将X卡交易给一名玩家可以正常工作,但是每张玩家的X卡都不能正常工作。这是我的输出:

('2', 'C') ('2', 'D') ('2', 'H') ('2', 'S')
('3', 'C') ('3', 'D') ('3', 'H') ('3', 'S')
...
('K', 'C') ('K', 'D') ('K', 'H') ('K', 'S')
('A', 'C') ('A', 'D') ('A', 'H') ('A', 'S')
[]
Player1 hand:  [('A', 'S')]
Player1 hand:  [('A', 'S'), ('A', 'H')]
Player1 hand:  [('A', 'S'), ('A', 'H'), ('A', 'D')]
('2', 'C') ('2', 'D') ('2', 'H') ('2', 'S')
...
('K', 'C') ('K', 'D') ('K', 'H') ('K', 'S')
('A', 'C')
[('A', 'S'), ('A', 'H'), ('A', 'D')]
debug 0
debug 1
debug 2
debug 3
p1

Traceback (most recent call last):
    File "C:\Users\11583\Desktop\Personal\cardgame.py", line 94, in <module>
        dealXtoEach( 3, d )
    File "C:\Users\11583\Desktop\Personal\cardgame.py", line 60, in dealXtoEach
        print player.hand
AttributeError: 'str' object has no attribute 'hand'

我可以说它不喜欢我试图将一个元组附加到由player全局变量中的字符串表示的Player实例的dealXtoEach函数内的引用。 Sheesh,我很困惑只是解释它。

那么我该如何解决这个问题呢?

由于

2 个答案:

答案 0 :(得分:2)

该错误与附加到元组无关(顺便提一下,players是一个列表而不是元组 - 它不可能附加到元组上。)

相反,请尝试替换该行:

players.append( name )

players.append( self )

这样,全局players列表将不包含字符串列表(您不能使用太多),它将包含Player个对象的列表(其中包含其他的东西,hand属性。)


额外注意事项:全局players列表并不是必需的(通常建议使用全局变量) - 您可以只传递players列表。其次,dealXToOnedealOneToEach应该是Deck类的方法。总之,这将使最后几行看起来像:

players = [p1, p2]
d.dealXToOne(3, p1)
d.dealXToEach(e, players)

答案 1 :(得分:1)

player列表中的每个players都是字符串,而不是Player对象,因为这一行:

players.append( name )

替换为:

players.append( self )