Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:not': 0.03; 'else:': 0.03; 'float': 0.07; 'false,': 0.09; 'false.': 0.09; 'mixed': 0.09; 'parameter': 0.09; 'python': 0.11; 'def': 0.12; '*any*': 0.16; '*only*': 0.16; 'calculator': 0.16; 'inputs': 0.16; 'integer.': 0.16; 'oppose': 0.16; 'picks': 0.16; 'scripts.': 0.16; 'u+2155,': 0.16; 'vulgar': 0.16; 'thursday,': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'value.': 0.19; 'entered': 0.20; '>>>': 0.22; 'input': 0.22; 'tests': 0.22; 'either.': 0.24; 'fraction': 0.24; 'integer': 0.24; 'unicode': 0.24; 'source': 0.25; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'am,': 0.29; 'characters': 0.30; 'errors': 0.30; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'decimal': 0.31; 'this.': 0.32; 'probably': 0.32; 'could': 0.34; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'false': 0.36; 'otherwise.': 0.36; 'possible': 0.36; 'implement': 0.38; 'thank': 0.38; 'checks': 0.38; 'e.g.': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'users': 0.40; 'according': 0.40; 'even': 0.60; 'skip:u 10': 0.60; 'ian': 0.60; 'first': 0.61; 'you.': 0.62; 'making': 0.63; '8bit%:61': 0.84; 'characters,': 0.84; 'subject:check': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=a3tusg0bw4oAwVjYC78HpLTWJRGQ1BI41w3oZgV/ttQ=; b=0/9IbM+GYhdetQYz/l4PiwSIEHi0ipz7iFWdfkzXrgbY2iYhw1Y0iQAi8LULL7682F mVXnF7enK19d1D+7X5DBLp2THGUFb0C01oroA/TghJ3qoj6vkaBKWv39gDb93lFrqEGm IExFjv7+aRV53GlsbQx+V0dLzHODcOMO4xIuGxYRYt7ApMMFFeI25JSmmJ6B1X6SXJL8 TYKJjGyxMUYKR/OPT0QneUBliZybwf3qd3AOOzS9Gm12VsUCYlcFSbkmYKYeb5EcxUK0 OfW8Ps4VHliiTI2G5zXooEad6Tx037btNjugj8h2oRb5Ww1KSJhBibr4qqVn8JSXa5EA hNoA== X-Received: by 10.236.8.103 with SMTP id 67mr5020751yhq.29.1403163511749; Thu, 19 Jun 2014 00:38:31 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <52ba65d1-a5dc-47b5-bccd-e05b24ae3e1f@googlegroups.com> From: Ian Kelly Date: Thu, 19 Jun 2014 01:37:50 -0600 Subject: Re: how to check if a value is a floating point or not To: Python Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1403163519 news.xs4all.nl 2962 [2001:888:2000:d::a6]:54529 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:73400 On Thu, Jun 19, 2014 at 1:23 AM, Ian Kelly wrote: > On Thu, Jun 19, 2014 at 12:48 AM, Nicholas Cannon > 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 all= ow it to check and use floating points. If you need the source code i am ha= ppy 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 te= st if it is normal int with the isnumeric function. Unfortunately this func= tion picks up the decimal as false. This means if the user inputs a float i= t has to be false. I then test if this input has any alphabetical character= s if it does not the user could have only entered something like 12.5 oppo= se 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. Incidentally, unicode.isnumeric is probably not what you want either. According to the docs, it returns "True if there are only numeric characters in S, False otherwise. Numeric characters include digit characters, and all characters that have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH." So that includes strings like u'123=E2=85=95=E2=85=93=E2=85=A7=D9=A4', which is clearly not = an integer. You'd likely do better with unicode.isdigit, and even then you'd be allowing for mixed scripts.