本题目要求根据以下分段函数的定义,计算输入的x对应的y值,输出结果保留两位小数;如果输入的x是非数值型数据,输出'Input Error'。注意:使用math库
输入格式:
在一行中输入x的值。
输出格式:
按“f(x) = result”的格式输出,其中x与result都保留两位小数,注意'='两边有空格。
如果输入的x是非数值型数据,输出:Input Error
输入样例1:
1.5
输出样例1:
f(1.50) = 4.55
输入样例2:
-4
输出样例2:
f(-4.00) = 0.00
输入样例3:
abc
输出样例3:
Input Error
代码
import math
from math import e//当要使用常数e时需要引用math库里的e
x=input()
y=0
if x.isalpha()://判定是否只由字符类型
print("Input Error")
else:?//如果不是就可能含数字类型
a=float(x)
if a>0:
y=math.cos(a)+pow(e,a)//用到三角函数时首先要引math库,其次要math.(三角函数)
elif a<=0:
y=0
print("f(%.2f) = %.2f"%(a,y))
a = 1
b = [1,2,3,4]
c = (1,2,3,4)
d = {'a':1,'b':2,'c':3}
e = "abc"
if isinstance(a,int):
print "a is int"
else:
print "a is not int"
if isinstance(b,list):
print "b is list"
else:
print "b is not list"
if isinstance(c,tuple):
print "c is tuple"
else:
print "c is not tuple"
if isinstance(d,dict):
print "d is dict"
else:
print "d is not dict"
if isinstance(e,str):
print "d is str"
else:
print "d is not str"