[Tutor] Python - RPG Combat System

Steven D'Aprano steve at pearwood.info
Sat Jul 31 06:00:35 CEST 2010


On Sat, 31 Jul 2010 12:49:36 pm Jason MacFiggen wrote:
> I am have trouble figuring out how to make my program stop at 0 hit
> points.... if I run it, it always goes into the negative hitpoints...
>
> So my question is how do I make this program end at exactly 0 hit
> points every time instead of going over?

I don't have much time to work on this now, but you should do something 
like:

while mo_hp > 0 or my_hp > 0:  
    # combat stops when either combatant is dead
    do_combat_round()
    if mo_hp <= 0:
        print "The Lich King has been slain!"
    if my_hp <= 0:
        print "You have been slain by the Lich King!"


> and also, what can I do instead of writing print so many times?

Don't write print so many times.

I don't understand the question. If you want to print 20 things, then 
you have to print 20 things. If that's too much printing, then only 
print 10 of them :)

I suppose you could lump two or three things together, in one call to 
print, by adding your own newlines into the strings. E.g. instead of

print "Menu Selections: "
print "1 - Attack"
print "2 - Defend"
print

you could write:

print "Menu Selections: \n1 - Attack\n2 - Defend\n\n"

but I don't really see that as an advantage. Probably better to wrap 
common code into a function, then call the function:

def print_menu():
    print "Menu Selections: "
    print "1 - Attack"
    print "2 - Defend"
    print

print_menu()
choice = choose_from_menu()

etc.


-- 
Steven D'Aprano


More information about the Tutor mailing list