python3学习(八)--小数校验

def check_float(s:str):
    if s.count('.')==1:
        a = s.split('.')[0]
        b = s.split('.')[1]
        if a.isdigit() and b.isdigit():
            print('%s 为正整数'%(s))
            return s
        else:
            if a.startswith('-') and a.count('-') == 1:
                print('%s 为负整数'%(s))
            return s
    else:
        print('非小数')

 check_float('1.5')


#正小数  1.5
#1、小数点个数必须为1-->'1.5'.count('.')==1
#2、小数点左右与两边都是整数-->'1.5'.split('.').isdigit()
#负小数  -1.5
#1、小数点个数必须为1-->'1.5'.count('.')==1
#2、小数点左右与两边都是整数-->'1.5'.split('.').isdigit()
#3、符号开头,并且只有一个负号-->'1.5'.startwith()

def check_float(s):
    '''
    这个函数的作用就是判断传入的字符串是否是合法的小数
    :param s: 传入一个字符串
    :return: True/False
    '''
    s = str(s)
    if s.count('.') == 1:
        s_split = s.split('.')
        #1.5  [1,5]
        left,right = s_split  #等价于left=s_splilt[0]   right = s_split[1]
        if left.isdigit() and right.isdigit():
            return True
        elif left.startswith('-') and left[1:].isdigit() and right.isdigit():
            return True
    return False  #假如传一个整数,例如4,直接走到该处


print(check_float(1.3))
print(check_float(-1.3))
print(check_float('1.3'))
print(check_float('-1.3'))
print(check_float('--1.2'))
print(check_float('2.3w3'))
print(check_float('22s.4'))

 

转载于:https://www.cnblogs.com/xm-sunnylin/p/9641797.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值