Using isdigit for floats?

前端 未结 8 1138
故里飘歌
故里飘歌 2020-12-05 09:59
a = raw_input(\'How much is 1 share in that company? \')

while not a.isdigit():
    print(\"You need to write a number!\\n\")
    a = raw_input(\'How much is 1 shar         


        
8条回答
  •  时光取名叫无心
    2020-12-05 10:29

    The provided answers fail if the string contains some special characters such as underscore (e.g. '1_1'). The following function returns correct answer in all case that I tested.

    def IfStringRepresentsFloat(s):
    try:
        float(s)
        return str(float(s)) == s
    except ValueError:
        return False
    

提交回复
热议问题