Need some simple coding help
Steven Bethard
steven.bethard at gmail.com
Wed Mar 2 09:48:03 EST 2005
mx2k wrote:
> indeed, we're it is our combat timeline. we enter the initiative and
> reaction time, it should find out the lowest then the next, then the
> 'n'th etc. and shouw it on the screen like
> Next action 'Character Name1' at tick No 'Initiative1
> 2nd action 'Character Name2' at tick No 'Initiative2'
> [...]
> 'n'th action 'Character Name n' at tick No 'Initiative n'
>
> Now you enter a value by which the initiative should be increased,
> then adding reaction time to that value and then adding the result to
> reaction time. Now soring again by lowest,next,'n'th, etc. and then
> again showing the output, asking for another number etc.
Not sure if I got your calculation right, but something like this should
work:
py> characters
[Character('Steve', 3, 2), Character('mx2k', 5, 1)]
py> def get_init(character):
... return (character.initiative + character.reaction_time +
... int(raw_input('%s initiative: ' % character.name)))
...
py> def print_initiatives(characters, rounds):
... for _ in range(rounds):
... inits = dict([(character, get_init(character))
... for character in characters])
... for character in sorted(inits, key=inits.__getitem__):
... print character, inits[character]
...
py> print_initiatives(characters, 3)
[... I type '5', '6' ...]
Character('Steve', 3, 2) 10
Character('mx2k', 5, 1) 12
[... I type '9', '1' ...]
Character('mx2k', 5, 1) 7
Character('Steve', 3, 2) 14
[... I type '1', '2' ...]
Character('Steve', 3, 2) 6
Character('mx2k', 5, 1) 8
If you want the characters in the opposite order, simply use
sorted(inits, key=inits.__getitem__, reverse=True)
instead of
sorted(inits, key=inits.__getitem__)
HTH,
STeVe
More information about the Python-list
mailing list