As noted, while dict is versatile, you'd be better of with a class.
Here's another quick and dirty example:
Python 3.2.3 (default, Jun 25 2012, 23:10:56)
[GCC 4.7.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from random import randint
>>>
>>>
>>> class Mob(object):
... def __init__(self, name, initMod=0):
... self.name = name
... self.initMod = initMod
...
... def rollInit(self):
... return randint(1,10) + self.initMod
...
>>>
>>> def getAttackOrder(mobs):
... order = []
... for mob in mobs:
... roll = mob.rollInit()
... while roll in order:
... roll = mob.rollInit()
... print(mob.name,'rolls',roll)
... order.append(roll)
... return [ y for (x,y) in sorted(zip(order, mobs), key=lambda x:x[0], reverse=True) ]
...
>>>
>>> def test(mobs, n):
... for i in range(n):
... print(','.join(x.name for x in getAttackOrder(mobs)))
... print('---------')
...
>>>
>>> mobs = [Mob('Alice',-2),Mob('Bob',1),Mob('Charlie'),Mob('Erik')]
>>>
>>> test(mobs,4)
Alice rolls 7
Bob rolls 5
Charlie rolls 4
Erik rolls 10
Erik,Alice,Bob,Charlie
---------
Alice rolls 2
Bob rolls 9
Charlie rolls 3
Erik rolls 10
Erik,Bob,Charlie,Alice
---------
Alice rolls 0
Bob rolls 7
Charlie rolls 4
Erik rolls 1
Bob,Charlie,Erik,Alice
---------
Alice rolls -1
Bob rolls 10
Charlie rolls 3
Erik rolls 6
Bob,Erik,Charlie,Alice
---------
>>>
Hope this helps.

New Topic/Question
Reply




MultiQuote



|