Python中的问题

我要怎么样才能巧妙地把运算符号全部表示(能把代码写出来吗?)万分感谢

img

a, b = input().split(',')
a = int(a)
b = int(b)
c = input()
if c == '+':
    print(a+b)
elif c == '-':
    print(a-b)
elif c == '*':
    print(a*b)
elif c == '/':
    if b==0:
        print('除数不能为0')
    else:
        print(a/b)
elif c == '//':
    print(a//b)
elif c == '**':
    print(a**b)
elif c == '%':
    print(a%b)

除了if elif else之外 ,没有别的办法
如果输入的数据都是合法的,你还可以偷偷的用eval来代替
但是输入的除数有0需要判断,这样搞就不行了

a, b = map(int, input().strip().split(','))
operation = input().strip()  # +、-、*、/、//、%、**
if operation == '/' and b == 0:
    print('除数不能为0')
elif operation == '%' and b == 0:
    print('不能对0取余')
else:
    print(eval(str(a) + operation + str(b)))

Python 简单计算器实现 | 菜鸟教程 https://www.runoob.com/python3/python3-calculator.html