I've just started using classes in Python.
The basic goal is to develop a script that tracks individual player stats for
poker tournaments. This involves nesting a class within a class within a
class. The Player class incorporates a Stats class (three instances for three
different game types) and the Stats class incorporates a Details class (three
instances for three different game phases).
In creating a Player instance, that instance can create the Stats class, but
when creating the first instance within the Stats class of a Details class, it
fails with the following traceback:
Traceback (most recent call last):
File
"C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line
309, in RunScript
debugger.run(codeObject, __main__.__dict__, start_stepping=0)
File "C:\Python25\Lib\site-packages\pythonwin\pywin\debugger\__init__.py",
line 60, in run
_GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
File "C:\Python25\Lib\site-packages\pythonwin\pywin\debugger\debugger.py",
line 624, in run
exec cmd in globals, locals
File "C:\Documents and Settings\Mike and Patsy\Desktop\pk\pkutilities.py",
line 140, in <module>
initplayers(playernames)
File "C:\Documents and Settings\Mike and Patsy\Desktop\pk\pkutilities.py",
line 119, in initplayers
gameplayers[name] = Player(name)
File "C:\Documents and Settings\Mike and Patsy\Desktop\pk\pkutilities.py",
line 8, in __init__
self.stat[0] = Stats('holdem', name)
File "C:\Documents and Settings\Mike and Patsy\Desktop\pk\pkutilities.py",
line 29, in __init__
self.gamephase[0] = Details('full')
IndexError: list assignment index out of range
The appropriate code segments defining the classes and the calling function
follow:
class Player():
def __init__(self,name):
self.name = name
self.stat[0] = Stats('holdem', name)
self.stat[1] = Stats('omahah', name)
self.stat[2] = Stats('omahahl', name)
class Stats():
def __init__(self, type, name):
self.name = name
self.gametype = type
self.totalgames = 0
self.totalgamestofinish = 0
self.totalfinish = 0
self.gamephase[0] = Details('full') # this is the line which fails
self.gamephase[1] = Details('mid')
self.gamephase[2] = Details('end')
class Details():
def __init__(self, type):
self.type = type
self.totalhands = 0
self.VPIPhands = 0
self.VPIPcount = 0
self.VPSBhands = 0
self.VPSBcount = 0
self.flopseen = 0
self.turnseen = 0
self.riverseen = 0
self.preflopraise = 0
self.flopraise = 0
self.turnraise = 0
self.riverraise = 0
self.winpreflop = 0
self.winflop = 0
self.winturn = 0
self.winriver = 0
# this is the function that creates the Player instance
def initplayers(playernames):
global db, gameplayers
db = shelve.open('playerstats.dat')
for i in range(len(playernames)):
name = playernames[i]
if db.has_key(name):
gameplayers[name] = db[name]
else:
gameplayers[name] = Player(name) # this line creates the Player
instance
db[name] = gameplayers[name]
I don't see how the "list assignment index" can be out of range, so I assume
there's an earlier error that I can't find or a syntax error that I'm
overlooking.
Can anyone point out where I'm going wrong?
And, if you can recommend a better way to organize the data, feel free.
Thanks for your help.
Mike
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor