3
-1
4
1
n1、n2 Must be an integer
n1、n2 Must be an integer
This function can only perform four operations
Divisor is 0, error
def fun(n1, n2, operation='+'):
try:
if not isinstance(n1,int) or not isinstance(n2,int):
raise Exception('n1、n2 Must be an integer ')
except Exception as e:
return e
try:
assert len(operation) == 1 and operation in "+-*/", " This function can only perform four operations "
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 ' Divisor is 0, error '
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, '/'))