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


Groups > comp.lang.python > #73455

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!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'subject:not': 0.03; 'beginner': 0.05; 'float': 0.07; 'exception,': 0.09; 'exception.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sanity': 0.09; 'thus,': 0.09; 'typed': 0.09; 'unhandled': 0.09; 'python': 0.11; '"to': 0.16; 'occurs.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sorts': 0.16; 'exception': 0.16; ':-)': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'advance.': 0.19; 'code,': 0.22; 'input': 0.22; 'coding': 0.22; 'saying': 0.22; 'header:User- Agent:1': 0.23; 'error': 0.23; 'example.': 0.24; 'instead.': 0.24; 'guys': 0.24; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'leave': 0.29; 'raise': 0.29; 'errors': 0.30; 'that.': 0.31; 'crash': 0.31; 'exceptions': 0.31; 'int,': 0.31; 'stuff': 0.32; 'style': 0.33; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'i.e.': 0.36; 'should': 0.36; 'error.': 0.37; 'checks': 0.38; 'e.g.': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'most': 0.60; 'tell': 0.60; 'conversion': 0.61; 'due': 0.66; 'dont': 0.67; 'repeat': 0.74; 'fails,': 0.84; 'subject:check': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Sturla Molden <sturla.molden@gmail.com>
Subject Re: how to check if a value is a floating point or not
Date Fri, 20 Jun 2014 13:16:32 +0000 (UTC)
References <d5ca21d7-23e6-4240-83d8-262d0f877f7e@googlegroups.com> <c21b1d32-a716-4629-aad6-357b62a8f529@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host 6-154-11.connect.netcom.no
User-Agent NewsTap/4.0.1 (iPad)
X-
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.11165.1403270214.18130.python-list@python.org> (permalink)
Lines 32
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1403270214 news.xs4all.nl 2918 [2001:888:2000:d::a6]:45197
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:73455

Show key headers only | View raw


Nicholas Cannon <nicholascannon1@gmail.com> wrote:

> Guys i am only a beginner at python most of the stuff you are saying i
> need to do i dont understand.

Then listen and try to learn :-)

In C it is customary to do all sorts of sanity checks in advance.
Validating user input is an example. We can call this "to ask permission".
This coding style is often neccessary in C, but not recommended in Python.

In Python we just try to do what we want. If it fails, we get an exception,
e.g. a ValueError. Then we do something with this error instead. We can
call this "to ask forgiveness". If you think you need a validator, you are
very likely thinking "unpythonic". Thus, we don't have to check that the
user typed in a float. We just try to construct a float from the input. If
it fails it wasn't convertible to a float. But you don't have to know that
in advance. All the checks you need to do is already in the function
float(). You don't have to repeat them. float() will succeed or raise an
error. Same for conversion to int: If the user input is convertible to int,
the function int() will do that. If it's not convertible, you get an
exception. Just trap the exception and deal with it when it occurs.

But don't use try/except everywhere! Some exceptions might be due to an
error in your own code, i.e. not in the user input. Those errors you should
not silence, but let your program crash and abort. Then you will know there
is an error in your code. That is what an unhandled exception will do, and
in addition it will tell you where the error is and what it is, so just
leave those exceptions unhandled.

Sturla

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