# 参考答案1
a=float(input("a="))
b=float(input("b="))
c=input("+-*/?")
if c=="+":
print(a+b)
elif c=="-":
print(a-b)
elif c=="*":
print(a*b)
else:
if b==0:
print("分母不可以为0!")
else:
print(a / b)
#参考答案二(纯属装逼用,不推荐,可读性太差)
a=float(input("a="))
b=float(input("b="))
c=input("+-*/?")
sum=(a+b) if c=="+" else ((a-b) if c=="-" else ((a*b) if c=="*" else (a/b)))
print(sum)
#参考答案3
a=float(input("a="))
b=float(input("b="))
c=input("+-*/?")
方案三推荐使用:
from __future__ import division # 使除非操作自动识别小数结果
def jia(x, y):
return x + y
def jian(x, y):
return x - y
def cheng(x, y):
return x * y
def chu(x, y):
return x / y
operator = {"+": jia, "-": jian, "*": cheng, "/": chu}
def f(x, o, y):
print(operator.get(o)(x, y)) # 类似switch函数操作
a=float(input("a="))
b=float(input("b="))
c=input("+-*/?")
f(a, c, b) # 函数调用