Python学习-比较数据类型

def typeof(variate):
    type = None
    if isinstance(variate,float):
        type = "float"
    elif isinstance(variate,str):
        type = "str"
    return type
def getType(variate):
     arr = {"int":"整数","float":"浮点","str":"字符串","list":"列表","tuple":"元组","dict":"字典","set":"集合"}
     vartype = typeof(variate)
     if not (vartype in arr):
         return "未知类型"
     return arr[vartype]
value = 2.1
if getType(value) == "浮点":
int_value = int(value)
else:
str_value = value
print(int_value)

 

posted @ 2021-11-11 14:28  小哈别闹  阅读(73)  评论(0)    收藏  举报