Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #73455
| From | Sturla Molden <sturla.molden@gmail.com> |
|---|---|
| Subject | Re: how to check if a value is a floating point or not |
| Date | 2014-06-20 13:16 +0000 |
| References | <d5ca21d7-23e6-4240-83d8-262d0f877f7e@googlegroups.com> <c21b1d32-a716-4629-aad6-357b62a8f529@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.11165.1403270214.18130.python-list@python.org> (permalink) |
Nicholas Cannon <nicholascannon1@gmail.com> wrote: > Guys i am only a beginner at python most of the stuff you are saying i > need to do i dont understand. Then listen and try to learn :-) In C it is customary to do all sorts of sanity checks in advance. Validating user input is an example. We can call this "to ask permission". This coding style is often neccessary in C, but not recommended in Python. In Python we just try to do what we want. If it fails, we get an exception, e.g. a ValueError. Then we do something with this error instead. We can call this "to ask forgiveness". If you think you need a validator, you are very likely thinking "unpythonic". Thus, we don't have to check that the user typed in a float. We just try to construct a float from the input. If it fails it wasn't convertible to a float. But you don't have to know that in advance. All the checks you need to do is already in the function float(). You don't have to repeat them. float() will succeed or raise an error. Same for conversion to int: If the user input is convertible to int, the function int() will do that. If it's not convertible, you get an exception. Just trap the exception and deal with it when it occurs. But don't use try/except everywhere! Some exceptions might be due to an error in your own code, i.e. not in the user input. Those errors you should not silence, but let your program crash and abort. Then you will know there is an error in your code. That is what an unhandled exception will do, and in addition it will tell you where the error is and what it is, so just leave those exceptions unhandled. Sturla
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
how to check if a value is a floating point or not nicholascannon1@gmail.com - 2014-06-18 22:53 -0700
Re: how to check if a value is a floating point or not Gary Herron <gary.herron@islandtraining.com> - 2014-06-18 23:22 -0700
Re: how to check if a value is a floating point or not Nicholas Cannon <nicholascannon1@gmail.com> - 2014-06-18 23:48 -0700
Re: how to check if a value is a floating point or not Ben Finney <ben@benfinney.id.au> - 2014-06-19 17:19 +1000
Re: how to check if a value is a floating point or not Ian Kelly <ian.g.kelly@gmail.com> - 2014-06-19 01:23 -0600
Re: how to check if a value is a floating point or not Ian Kelly <ian.g.kelly@gmail.com> - 2014-06-19 01:37 -0600
Re: how to check if a value is a floating point or not Sturla Molden <sturla.molden@gmail.com> - 2014-06-19 13:46 +0000
Re: how to check if a value is a floating point or not Nicholas Cannon <nicholascannon1@gmail.com> - 2014-06-19 23:14 -0700
Re: how to check if a value is a floating point or not Ian Kelly <ian.g.kelly@gmail.com> - 2014-06-20 00:22 -0600
Re: how to check if a value is a floating point or not Sturla Molden <sturla.molden@gmail.com> - 2014-06-20 13:16 +0000
Re: how to check if a value is a floating point or not Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-06-20 14:40 +0100
Re: how to check if a value is a floating point or not Grant Edwards <invalid@invalid.invalid> - 2014-06-20 14:28 +0000
Re: how to check if a value is a floating point or not alister <alister.nospam.ware@ntlworld.com> - 2014-06-20 15:15 +0000
Re: how to check if a value is a floating point or not Ian Kelly <ian.g.kelly@gmail.com> - 2014-06-20 09:44 -0600
csiph-web