带有 Aptana IDE 的 Python 3.3 版
我在工作之余自学python,所以我决定重新创建一个我和朋友玩的扑克游戏。为了方便对不同函数的各种调用,我创建了两个对象。一个对象包含整个游戏的数据 GameData,第二个对象包含一个玩家 Player,我试图将其添加到 GameData。
下面的代码循环并尝试将 Player 添加到 GameData 中的列表中,但我收到一条错误消息:
AttributeError:类型对象“GameData”没有属性“玩家”
class Player(object):
def __init__(self, seat):
self.seat = seat
self.chips = 0
self.wins = 0
self.card = 0
self.isDealer = False
class GameData(object):
def __init__(self):
self.games = 0
self.numPlayers = 0
self.chips = 0
self.players = []
self.deck = []
下面的函数抛出错误
def testDealCards():
gd = nuts.GameData #declare GameData object
gd.deck = [7,5,5,5,3,1,5,6,1,2] #assign values to deck list
for x in range(2): #loop to create player objects in GameData
gd.players.append(Player)
gd.players[0].isDealer = True
gd.players[1].isDealer = False
print(gd.players)
nuts.dealCards(gd)
assert gd.players[0].card == 5
assert gd.players[1].card == 7
pass