Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #73398

Re: how to check if a value is a floating point or not

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 <python-python-list@m.gmane.org>
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 <ben@benfinney.id.au>
Subject Re: how to check if a value is a floating point or not
Date Thu, 19 Jun 2014 17:19:44 +1000
References <d5ca21d7-23e6-4240-83d8-262d0f877f7e@googlegroups.com> <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 <bignose+hates-spam@benfinney.id.au>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.11132.1403162399.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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