import random
deffunction(x,op,y):try:#将x,y转换为浮点数
x,y=float(x),float(y)#进行四则运算if op=='+':print('{}+{}={}'.format(x,y,x+y))elif op=='-':print('{}-{}={}'.format(x,y,x-y))elif op=='*':print('{}*{}={}'.format(x,y,x*y))elif op=='/':#判断y是否为0if y==0:print("y connot be 0")else:print("{}/{}={}".format(x,y,x/y))#四则运算符出错else:print("please enter right operator!")#不能转换为浮点数except ValueError:print("please enter right Floating point number!")#输入两个数字和符号,以空格进行切割
x,op,y=map(str,input("print input x,op,y:").split(' ',2))
function(x,op,y)
运行结果:
printinput x,op,y:4/54.0/5.0=0.8printinput x,op,y:4 s 5
please enter right operator!
printinput x,op,y:4/0
y connot be 0