python: checking for errors in the users input
Question: I would like to check if a string can be a ﬂoat before I attempt to convert it
to a ﬂoat. This way, if the string is not ﬂoat, we can print an error message
and exit instead of crashing the program. so when the user inputs something, I
wanna see if its a float so it will print "true" if its not then it will
print"false" rather than crashing. I don't want to use built in functions for
this. I need to make my own function for this.

I tried : import types

    
    
    def isFloat():
        x = raw_input("Enter: ")
        if(x) == float:
            print("true")
    
        if(x) == str:
            print("false")
    
    isFloat()
    

I don't know if its true or not but it wont work it wont print anything either


Answer: The recommended thing to do here is to `try` it:

    
    
    try:
        f = float(x)
        print("true")
    except ValueError:
        # handle error here
        print("false")
    

This underscores the Python philosophy _"It's better to ask for forgiveness
than for permission"_.

