[Tutor] sys.exit help

Jason MacFiggen jmacfiggen at gmail.com
Sun Aug 1 22:26:24 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?

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)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100801/e6c7a253/attachment.html>


More information about the Tutor mailing list