#1、必須只有一個小數點 #2、小數點的左邊必須是整數,小數點的右邊必須是正整數 def is_float1(s=None): s = str(s) #.1 if s.count('.')==1: left,right = s.split('.') #['-','1'] if left.isdigit() and right.isdigit() and int(right)>0:#判斷正小數 return True elif left.startswith('-') and left.count('-')==1 and right.isdigit() and int(right)>0: #先判斷負號開頭,只有一個負號,小數點右邊是整數 lleft = left.split('-')[1] #如果有負號的話,按照負號分隔,取負號后面的數字 if lleft.isdigit():#判斷左邊負號后邊是整數 return True return False print(is_float1(-111111.0)) print(is_float1('s.1')) print(is_float1('...1')) print(is_float1('1.s')) print(is_float1(-1.1)) 結果: False False False False True
