On Fri, Jul 30, 2010 at 10:49 PM, Jason MacFiggen <jmacfig...@gmail.com> 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? > > and also, what can I do instead of writing print so many times? > > import random > my_hp = 50 > mo_hp = 50 > my_dmg = random.randrange(1, 20) > mo_dmg = random.randrange(1, 20)
You ask for a random number > while True: > if mo_hp < 0: > print "The Lich King has been slain!" > elif my_hp < 0: > print "You have been slain by the Lich King!" > if mo_hp <= 0: > break > elif my_hp <= 0: > break > else: > print "Menu Selections: " > print "1 - Attack" > print "2 - Defend" > print > choice = input ("Enter your selection. ") > choice = float(choice) > print > if choice == 1: > mo_hp = mo_hp - my_dmg > print "The Lich King is at ", mo_hp, "Hit Points" > print "You did ", my_dmg, "damage!" > print > my_hp = my_hp - mo_dmg > print "I was attacked by the lk for ", mo_dmg," damage!" > print "My Hit Points are ", my_hp > print > elif choice == 2: > mo_hp = mo_hp - my_dmg / 2 > print "The Lich King is at", mo_hp, "Hit Points" > print "you did ", my_dmg / 2, "damage!" > print Then you your points by that random number, so if the random subtracted from current hp is negative it will print that negative when you ask it to print hp - mydmg. So, you add another if hp <= 0, in addition to the if my mydmg>myhp, then print 0, instead of hp - myd_mg > my_hp = my_hp - mo_dmg > print "I was attacked by the lk for ", mo_dmg," damage!" > print "My Hit Points are ", my_hp > print > > _______________________________________________ > Tutor maillist - tu...@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor