python之判定输入的数据类型

本题目要求根据以下分段函数的定义,计算输入的x对应的y值,输出结果保留两位小数;如果输入的x是非数值型数据,输出'Input Error'。注意:使用math库

Snap5.bmp

 

 

输入格式:

在一行中输入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"

 

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值