Detect Floating Point Number

Sort by

recency

|

559 Discussions

|

  • + 0 comments

    This prints True for '0' when I submit code

    print(True if re.match(r"^[-+\.]?\d*\.\d*$", input()) else False)
    

    This prints False for '0' when I submit code

    print(True if re.match(r"^[-+\.]?\d*\.?\d*$", input()) else False)
    

    what am I missing here?

    It should be the reverse, i.e. second statement above should be "True" (see regex101.com link) https://regex101.com/r/dIOXFe/1

  • + 0 comments
    import re 
    N = int(input())
    
    for _ in range(N):
        print(True if re.match(r"[+-]?[0-9]*\.[0-9]+$", input()) else False)
    
  • + 0 comments

    T = int(input())

    for n in range(1, T+1):

    skip_rest = False
    string_value = input()
    
    try:
        float(string_value)
    except ValueError:
        print(False)
        continue
    
    split_string_value = list(string_value)
    
    count = 0
    Is_alpha = 0
    for j in string_value:
    
        if j == ".":
            if j == string_value[-1]:
                print(False)
                skip_rest = True
                break
            count += 1
        if j.isalpha() is True:
            Is_alpha += 1
    
    if skip_rest is True:
        continue
    elif count== 1 and Is_alpha == 0:
        print(True)
    else:
        print(False)
    
  • + 0 comments

    x = int(input()) lst = [input() for i in range(x)] for i in lst: if i == '0': print("False") try: if float(i): print("True") except: print("False")

  • + 0 comments

    Can't we use while !