尤其看看除法中除数不为零怎样写

img

def func(operator, x, y):
  if operator == '+':
    result = x + y
  if operator == '-':
    result = x - y
  if operator == '*':
    result = x * y
  if operator == '/':
    if y == 0:
      print('y=0, 无法进行除法')
      else:
        result = x / y
  return result
num1 = int(input())
num2 = int(input())
num1 = int(input('num1: '))
num2 = int(input('num2: '))
operator = input('operator: ')
while operator not in ['+', '-', '*', '/']:
    print('计算符号只能是 + - * / !')
    operator = input('operator: ')
else:
    if operator == '+':
        result = num1 + num2
    elif operator == '-':
        result = num1 - num2
    elif operator == '*':
        result = num1 * num2
    elif operator == '/':
        while not num2:
            print('除数不能为0!')
            num2 = int(input('num2: '))
            # raise ZeroDivisionError('除数不能为0!')
        else:
            result = num1 / num2
    else:
        result = ''
        # raise ValueError('计算符号只能是 + - * / !')
print(num1, operator, num2, '=', result)