3
-1
4
1
n1、n2必須為整數
n1、n2必須為整數
此函數只能完成四則運算
除數為0,錯誤
def fun(n1, n2, operation='+'):
try:
if not isinstance(n1,int) or not isinstance(n2,int):
raise Exception('n1、n2必須為整數')
except Exception as e:
return e
try:
assert len(operation) == 1 and operation in "+-*/", "此函數只能完成四則運算"
except Exception as e:
return e
if operation == '+':
return n1 + n2
elif operation == '-':
return n1 - n2
elif operation == '*':
return n1 * n2
elif operation == '/':
try:
return n1 // n2
except:
return '除數為0,錯誤'
print(fun(1, 2))
print(fun(1, 2, '-'))
print(fun(2, 2, '*'))
print(fun(2, 2, '/'))
print(fun(2, 2.0, '/'))
print(fun(2, 'a', '/'))
print(fun(2,2,'**'))
print(fun(2, 0, '/'))