Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #73399
| References | <d5ca21d7-23e6-4240-83d8-262d0f877f7e@googlegroups.com> <52ba65d1-a5dc-47b5-bccd-e05b24ae3e1f@googlegroups.com> |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | 2014-06-19 01:23 -0600 |
| Subject | Re: how to check if a value is a floating point or not |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.11133.1403162953.18130.python-list@python.org> (permalink) |
On Thu, Jun 19, 2014 at 12:48 AM, Nicholas Cannon <nicholascannon1@gmail.com> wrote: > On Thursday, June 19, 2014 1:53:31 PM UTC+8, Nicholas Cannon wrote: >> I am making a calculator and i need it to support floating point values but i am using the function isnumeric to check if the user has entered an int value. I need the same for floating point types so i could implement an or in the if statement that checks the values the user has entered and allow it to check and use floating points. If you need the source code i am happy to give it to you. Thank you for your help > > I am using python 2.7.7 and i have come up with away but there is still possible errors for this. What i did was i this > > #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 > > The parameter a is the users input by the raw_input function. I first test if it is normal int with the isnumeric function. Unfortunately this function picks up the decimal as false. This means if the user inputs a float it has to be false. I then test if this input has any alphabetical characters if it does not the user could have only entered something like 12.5 oppose to abc.d. unicode.isalpha does not test if the input has *any* alphabetic characters. It tests if the input is *only* alphabetic characters. u'12.5'.isalpha() does return False. u'abc.d'.isalpha() *also* returns False, because the decimal point is not alphabetic. I second Gary Herron's suggestion to just try converting the value and catch the exception if it fails. Python already knows how to do this for you; there's no need to reinvent the wheel.
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