Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1a.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; 'parameter': 0.09; 'python': 0.11; 'def': 0.12; '*any*': 0.16; '*only*': 0.16; 'calculator': 0.16; 'fails.': 0.16; 'inputs': 0.16; 'oppose': 0.16; 'picks': 0.16; 'reinvent': 0.16; 'exception': 0.16; 'thursday,': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'value.': 0.19; 'entered': 0.20; 'input': 0.22; 'tests': 0.22; 'integer': 0.24; 'source': 0.25; 'second': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'am,': 0.29; 'characters': 0.30; 'converting': 0.30; 'errors': 0.30; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'decimal': 0.31; 'gary': 0.31; 'this.': 0.32; 'could': 0.34; 'knows': 0.35; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'false': 0.36; 'possible': 0.36; 'implement': 0.38; 'thank': 0.38; 'checks': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'users': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'catch': 0.60; 'first': 0.61; 'you.': 0.62; 'making': 0.63; 'subject:check': 0.84; 'you;': 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=us1chV5fYAN30V6+X4EFHKUzp0svwcKCJAq3dBHNqOs=; b=zb9izGouGGqsA/JUOJ8L8zWtKTsg9csWJ4VXWzO9GLcIGlntjtnLJGx/4W/7jbXiri VDOcCu/zoWnWXLzK4QiauI6qN7adLhVuVuuWbHaO4CpTM4vGv5C/OUbFBWecF3DWMY1D 2RpTUbOaPYASC+cPp8fGzFVnltHksi0U55tzNYgy4Hjq6NIyTqTGyBTSQfSGeBGcS1ys 1UsYcHMwubpjK2VVSXxUu+YxkqQ/GAc1ZXCzoZy0cq76WqXVTkR4JzmQ3WgZ5pDgTijm 96koqwY29xe9sqYzw4nYiSoyCUW11YU2sItvvVU7dpQEoQ2Y6ktARvDSs/qQu4AvQxf/ Yywg== X-Received: by 10.236.154.136 with SMTP id h8mr4714007yhk.105.1403162643369; Thu, 19 Jun 2014 00:24:03 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <52ba65d1-a5dc-47b5-bccd-e05b24ae3e1f@googlegroups.com> References: <52ba65d1-a5dc-47b5-bccd-e05b24ae3e1f@googlegroups.com> From: Ian Kelly Date: Thu, 19 Jun 2014 01:23:23 -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: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1403162953 news.xs4all.nl 2971 [2001:888:2000:d::a6]:51721 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:73399 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 i= nt 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 allo= w it to check and use floating points. If you need the source code i am hap= py 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 p= ossible 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 tes= t if it is normal int with the isnumeric function. Unfortunately this funct= ion 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 oppos= e 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.