我对用Python编程非常陌生,我搜索到的大多数答案都是比我想要的更复杂的脚本版本。我做了一个非常简单的计算器: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()
现在我想知道如何除以0并返回打印的“除以0错误”,而不是它现在所做的,即退出程序。除以0时的错误是:
^{pr2}$