[Tutor] Problem with program ending

a polite punk spacedweller@hotmail.com
Fri, 12 Jul 2002 18:44:19 -0400


Hi,

My name is Jonathan and I am a new Python programmer!  But, I am having 
trouble with a certain practice program that I have written.  It deals with 
functions and the try and except statements.  The trouble is with ending the 
program.  I use these commands below:

                         import sys
                         sys.exit()



but when i do, it ignores it and continues with the program in the main 
function.  I think it has to do with the use of the try and except 
statements since i do not have this problem in the programs without the try 
and except statements.  Here is the source code of the program below:



#Practice program with functions
#
#

def multiplyTwoNumbers(firstNumber, secondNumber):
    answer = firstNumber * secondNumber
    print "\n\n"
    print "The answer is:  ", answer
    print "\n\n"
    try:
        quitOrContinue = int(raw_input("Press 1 to continue or 0 to quit:  
"))
        if quitOrContinue == 1:
            main()

        elif quitOrContinue == 0:
            import sys
            sys.exit()

        else:
            main()

    except:
        print "\n\nYou must enter either 1 or 0"
        print
        main()



def divideTwoNumbers(firstNumber, secondNumber):
    answer = firstNumber / secondNumber
    print "\n\n"
    print "The answer is:  ", answer
    try:
        quitOrContinue = int(raw_input("Press 1 to continue or 0 to quit:  
"))
        if quitOrContinue == 1:
            main()

        elif quitOrContinue == 0:
            import sys
            sys.exit()

        else:
            main()

    except:
        print "\n\nYou must enter either 1 or 0"
        print
        main()



def subtractTwoNumbers(firstNumber, secondNumber):
    answer = firstNumber - secondNumber
    print "\n\n"
    print "The answer is:  ", answer
    try:
        quitOrContinue = int(raw_input("Press 1 to continue  or 0 to quit:  
"))
        while quitOrContinue != 1 or quitOrContinue != 0:
            print "You must enter either 1 or 0"
            try:
                quitOrContinue = int(raw_input("Press 1 to continue or 0 to 
quit:  "))
                if quitOrContinue == 1:
                    main()

                elif quitOrContinue == 0:
                    import sys
                    sys.exit()

                else:
                    main()

            except:
                print "You must enter valid choices"

    except:
        print "\n\nYou must enter either 1 or 0"


def addTwoNumbers(firstNumber, secondNumber):
    answer = firstNumber + secondNumber
    print "\n\n"
    print "The answer is:  ", answer

def main():
    print "\n\n"
    print "Press 1 to multiply two numbers"
    print "Press 2 to divide two numbers"
    print "Press 3 to subtract two numbers"
    print "Press 4 to add two numbers"
    print "Press 0 to quit"
    print
    try:
        choice = int(raw_input("Enter your selection:  "))
        try:
            firstNumber = int(raw_input("Enter the first number:  "))
            try:
                secondNumber = int(raw_input("Enter the second number:  "))
                if choice == 1:
                    multiplyTwoNumbers(firstNumber, secondNumber)

                elif choice == 2:
                    divideTwoNumbers(firstNumber, secondNumber)

                elif choice == 3:
                    subtractTwoNumbers(firstNumber, secondNumber)

                elif choice == 4:
                    addTwoNumbers(firstNumber, secondNumber)

                elif choice == 0:
                    import sys
                    sys.exit()

                else:
                    print
                    print "Invalid entry"
                    main()

            except:
                print "\n\nYou must enter an integer only"
                print
                main()

        except:
            print "\n\You must enter an integer only"
            print
            main()

    except:
        print "\n\nYou must enter integers only"
        print
        main()

main()






_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com