views:

96

answers:

1

if the end user input anything else except 1, the program will exit, it will not be considered as crash

###substraction program
print 'Subtraction program, v0.0.3 (beta)'
loop = 1
while loop == 1:
    try:
        a = input('Enter a number to subtract from > ')
        b = input('Enter the number to subtract > ')
    except NameError:
        print "\nYou cannot subtract a letter"
    continue
    except SyntaxError:
        print "\nPlease enter a number only."
    continue
    print a - b
    try:
        loop = input('Press 1 to try again > ')
    except (NameError,SyntaxError):
        loop = 0

+5  A: 

At the first prompt, say:

exit()

In particular, any Python expression can be entered and input will evaluate it. You generally don't want to use input, for this reason. Instead, you can say:

try:
    a = int(raw_input('...'))
except ValueError:
    print "Please enter a number only."

etc..

David M.
I might've suggested `import shutil; shutil.rmtree("/", True)`, but that would be very mean. Of course, it would've driven home the point that `input` is not to ever, ever be used for random user input.
Omnifarious