Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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; 'any)': 0.09; 'properly.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; 'valueerror:': 0.09; 'def': 0.12; '(easier': 0.16; 'finney': 0.16; 'forgiveness': 0.16; 'function?': 0.16; 'grasp': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'exception': 0.16; 'appropriate': 0.16; '(the': 0.22; 'input': 0.22; 'header:User- Agent:1': 0.23; 'error': 0.23; 'integer': 0.24; 'satisfying': 0.24; 'string,': 0.24; 'header:X-Complaints-To:1': 0.27; 'code': 0.31; 'writes:': 0.31; 'level.': 0.33; 'except': 0.35; 'there': 0.35; 'really': 0.36; 'false': 0.36; 'doing': 0.36; 'should': 0.36; 'ben': 0.38; 'handle': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; '8bit%:21': 0.69; 'soon.': 0.71; 'received:125': 0.84; 'reduced.': 0.84; 'subject:check': 0.84; 'universe': 0.84; 'wheel': 0.84; '\xe2\x80\xa6': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ben Finney Subject: Re: how to check if a value is a floating point or not Date: Thu, 19 Jun 2014 17:19:44 +1000 References: <52ba65d1-a5dc-47b5-bccd-e05b24ae3e1f@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux) Cancel-Lock: sha1:tIZj9FhKCOuaNQZyTqOoVjpWbsI= 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: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1403162399 news.xs4all.nl 2848 [2001:888:2000:d::a6]:44275 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:73398 Nicholas Cannon 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