python: checking for errors in the users input -


i check if string can float before attempt convert float. way, if string not float, can print error message , exit instead of crashing program. when user inputs something, wanna see if float print "true" if not print"false" rather crashing. don't want use built in functions this. need make own function 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 true or not wont work wont print either

the recommended thing here try it:

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

this underscores python philosophy "it's better ask forgiveness permission".


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -