I'm very new to programming in Python and most of the answers I have searched for are more complex versions of the script than I am looking for. I have made very simplistic calculator:
loop = 1
choice = 0
while loop == 1:
print 'Python Calculator!\n'
print 'Mathematical Operation:\n'
print '1) Addition'
print '2) Subtraction'
print '3) Multiplication'
print '4) Division'
print '5) Quit Python Calculator'
choice = input('Choose your operation: ')
if choice == 1:
add1 = input('Add:')
add2 = input('to: ')
print add1, '+', add2, '=', add1 + add2
elif choice == 2:
sub2 = input('Subtract: ')
sub1 = input('from: ')
print sub1, '-', sub2, '=', sub1-sub2
elif choice == 3:
mul1 = input('Multiply: ')
mul2 = input('by: ')
print mul1, '*', mul2, '=', mul1 * mul2
elif choice == 4:
div1 = input('Divide: ')
div2 = input('by: ')
print div1, '/', div2, '=', div1/div2
elif choice == 5:
print 'GOODBYE'
exit()
Now I am wondering how I can divide by zero and return the print "Divide by 0 Error" rather than what it does now, which is exit the program. The error I get when dividing by 0 is:
Traceback (most recent call last):
File "./calculator.py", line 32, in <module>
print div1, '/', div2, '=', div1/div2 ZeroDivisionError:
integer division or modulo by zero