[Tutor] sys.exit help
Evert Rol
evert.rol at gmail.com
Sun Aug 1 22:38:10 CEST 2010
> I was wondering how can I change sys.exit so if you use command line to run the program. it prompts a message asking if the user wants to exit instead of automatically just exiting?
Just write a wrapper exit() function around sys.exit that does that.
You don't want to change the sys.exit function to behave differently; it's purpose is simply to exit the program with a return value.
> import random
> import sys
> my_hp = 50
> mo_hp = 50
> menu1 = """
> Menu Selections:
> 1 - Attack
> 2 - Defend
> 3 - Help
> 4 - Exit
> """
> print menu1
> while True:
> my_dmg = random.randrange(1, 20)
> mo_dmg = random.randrange(1, 20)
>
> choice = input ("\nEnter your selection. ")
> choice = float(choice)
> print
> if choice == 1:
> mo_hp = mo_hp - my_dmg
> if mo_hp <= 0:
> print "Iris is at 0 Hit Points!\nYOU HAVE SLAIN IRIS!"
> sys.exit(0)
> elif mo_hp > 0:
> print "-----------------------------------------------------------------"
> print "Iris is at %s Hit Points\nYou did %s damage\n" % (mo_hp, my_dmg)
> my_hp = my_hp - mo_dmg
> if my_hp <= 0:
> print "Your Hit Points are 0!\nYOU HAVE BEEN SLAIN BY Iris!"
> sys.exit(0)
> elif my_hp > 0:
> print name,"was attacked by Iris for %s damage!\nMy Hit Points are %s" % (mo_dmg, my_hp)
> print "-----------------------------------------------------------------"
> else:
> print menu1
>
>
> elif choice == 2:
> mo_hp = mo_hp - my_dmg / 2
> if mo_hp <= 0:
> print "The Lich King is at 0 Hit Points!\nYOU HAVE SLAIN IRIS!"
> sys.exit(0)
> elif mo_hp > 0:
> print "-----------------------------------------------------------------"
> print "The Lich King is at %s Hit Points\nYou did %s damage\n" % (mo_hp, my_dmg)
> my_hp = my_hp - mo_dmg / 2
> if my_hp <= 0:
> print "Your Hit Points are 0!\nYOU HAVE BEEN SLAIN BY IRIS!"
> sys.exit(0)
> elif my_hp > 0:
> print name,"was attacked by the Iris for %s damage!\nMy Hit Points are %s" % (mo_dmg, my_hp)
> print "-----------------------------------------------------------------"
> else:
> print menu1
>
> elif choice == 3:
> print """
> -------------------------------------------------------------
> Attack = Attack for 100% of damage.
> Defending = Attack for 50% of damage and endure 50% of damage.
> -------------------------------------------------------------
> """
> elif choice == 4:
> sys.exit(0)
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list