Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #73398
| From | Ben Finney <ben@benfinney.id.au> |
|---|---|
| Subject | Re: how to check if a value is a floating point or not |
| Date | 2014-06-19 17:19 +1000 |
| References | <d5ca21d7-23e6-4240-83d8-262d0f877f7e@googlegroups.com> <52ba65d1-a5dc-47b5-bccd-e05b24ae3e1f@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.11132.1403162399.18130.python-list@python.org> (permalink) |
Nicholas Cannon <nicholascannon1@gmail.com> writes:
> #checks if the user input is an integer value
> def checkint(a):
> if a.isnumeric():
> return True
> else:
> if a.isalpha():
> return False
> else:
> return True
What code will be using this function? Why would that not be better
replaced with a ‘try … except’ construction?
That is, don't do this (Look Before You Leap)::
foo = get_a_string()
if checkint(foo):
bar = int(foo)
else:
bar = None
Instead, do this (Easier to Ask Forgiveness than Permission)::
foo = get_a_string()
try:
bar = int(foo)
except ValueError:
bar = None
If you need to create an integer based on a string, just do it, and
handle the exception (if any) at an appropriate level.
> There is one annoying error doing it this way and that is if you enter
> 12.ab or ab.12 it will say that it is okay. Still working on this so
> this should get sorted out soon.
You are re-inventing a wheel (the ‘int’ callable) which already does all
of that properly. Make use of it, and your frustration will be reduced.
--
\ “It is far better to grasp the universe as it really is than to |
`\ persist in delusion, however satisfying and reassuring.” —Carl |
_o__) Sagan |
Ben Finney
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