![图片[1]-python判断是否是小数-uusu优素-乐高,模型,3d打印,编程](http://uusucn.zbbe.cn/wp-content/uploads/2024/01/5e730ca302651455.jpg)
一、判断一个数是否为小数
1、有且仅有一个小数点
2、小数点的左边可能为正数或负数
3、小数点的右边为正数
二、实现代码
defis_float(str):
ifstr.count('.')==1:#小数有且仅有一个小数点
left=str.split('.')[0]#小数点左边(整数位,可为正或负)
right=str.split('.')[1]#小数点右边(小数位,一定为正)
lright=''#取整数位的绝对值(排除掉负号)
ifstr.count('-')==1andstr[0]=='-':#如果整数位为负,则第一个元素一定是负号
lright=left.split('-')[1]
elifstr.count('-')==0:
lright=left
else:
print('%s不是小数'%str)
ifright.isdigit()andlright.isdigit():#判断整数位的绝对值和小数位是否全部为数字
print('%s是小数'%str)
else:
print('%s不是小数'%str)
else:
print('%s不是小数'%str)
30.112是小数 -300.123是小数 -.5不是小数 2-1不是小数 --11..22不是小数 5.不是小数 不是小数 abc.efg不是小数原文来自:https://www.py.cn
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END



















































暂无评论内容