如何判断出一个字符串是否是小数??【python】

"""
小数的特征:有一位“.”,正小数、负小数。其他输入为非小数点
练习判断一个小数
1、判断是否合法
2、合法需要有一个小数点
3、小数点左边必须是个整数,右边必须是个正整数

"""

def xiaoshu(s):
xiaoshu_new=str(s)

if xiaoshu_new.count(".") ==1:
left,right = xiaoshu_new.split(".")
if left.isdigit() and right.isdigit():
print(1111)
return True

elif left.startswith('-') and left.count('-') == 1 and right.isdigit():
lleft = left.split('-')[-1]
if lleft.isdigit():
return True

return False


a = xiaoshu(-.1)

print(a)
posted @ 2019-09-26 01:13  遇见最好的你_juan  阅读(820)  评论(0编辑  收藏  举报