valueerrorcould not convert string to floatsomething suppose we wanted version of float that fails gracefullyreturning the input argument we can do this by writing function that encloses the call to float in tryexcept blockdef attempt_float( )tryreturn float(xexceptreturn the code in the except part of the block will only be executed if float(xraises an exceptionin [ ]attempt_float(' 'out[ ] in [ ]attempt_float('something'out[ ]'somethingyou might notice that float can raise exceptions other than valueerrorin [ ]float(( )typeerror traceback (most recent call lastin (---- float(( )typeerrorfloat(argument must be string or number you might want to only suppress valueerrorsince typeerror (the input was not string or numeric valuemight indicate legitimate bug in your program to do thatwrite the exception type after exceptdef attempt_float( )tryreturn float(xexcept valueerrorreturn we have thenin [ ]attempt_float(( )typeerror traceback (most recent call lastin (---- attempt_float(( )in attempt_float( def attempt_float( ) try---- return float( except valueerror return typeerrorfloat(argument must be string or number the basics www it-ebooks info