Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Test for number?

10 views
Skip to first unread message

Dr. Pastor

unread,
Sep 4, 2006, 1:39:56 PM9/4/06
to
In the following code I would like to ascertain
that x has/is a number. What the simplest TEST should be?
(Could not find good example yet.)
---
x=raw_input('\nType a number from 1 to 20')
if TEST :
Do_A
else:
Do_B
---
Thanks for any guidance.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Tim Williams

unread,
Sep 4, 2006, 2:25:11 PM9/4/06
to Dr. Pastor, pytho...@python.org
On 04/09/06, Dr. Pastor <el...@adsihqx.com> wrote:
> In the following code I would like to ascertain
> that x has/is a number. What the simplest TEST should be?
> (Could not find good example yet.)
> ---
> x=raw_input('\nType a number from 1 to 20')
> if TEST :
> Do_A
> else:
> Do_B
> ---

Something simple like the following might help,

>>> def numtest():
... x=raw_input('\nType a number from 1 to 20')
... try:
... x=int(x)
... print x, "is a number between 1 & 20 "
... except:
... print x, "is not a number"
...
>>> numtest() # enter 1
1 is a number between 1 & 20
>>> numtest() # enter "f"
f is not a number

its not a final solution though, think input = -2, 5.5 or 21

HTH :)

Helmut Jarausch

unread,
Sep 4, 2006, 2:54:38 PM9/4/06
to

One step further

try:
eval(x+'0')
....


--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany

Erik Max Francis

unread,
Sep 4, 2006, 3:11:43 PM9/4/06
to
Helmut Jarausch wrote:

> One step further
>
> try:
> eval(x+'0')
> ....

That is an exceedingly bad idea. Type:

__import__('sys').exit(),

in the prompt and see what happens.

You _never_ want to run `eval` on an untrusted string. Never.

--
Erik Max Francis && m...@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Divorces are made in Heaven.
-- Oscar Wilde

Hardcoded Software

unread,
Sep 4, 2006, 3:15:26 PM9/4/06
to

To test if it *is* a number, the solution is pretty simple (s.isdigit()
or an int(s) in a try..except ValueError), but if you want to test if
it is or *has* a number, I think that the simplest solution would be a
regexp:

import re
re_has_digit = re.compile(r'\d+')
try:
digits = re_has_digit.findall(input)[0]
number = int(digits)
print "%d is a number." % number
except IndexError:
print "'%s' has no number in it." % input

I didn't try it, but it should work.

George Sakkis

unread,
Sep 4, 2006, 3:48:29 PM9/4/06
to
Dr. Pastor wrote:

> In the following code I would like to ascertain
> that x has/is a number. What the simplest TEST should be?
> (Could not find good example yet.)
> ---
> x=raw_input('\nType a number from 1 to 20')
> if TEST :
> Do_A
> else:
> Do_B
> ---
> Thanks for any guidance.

x=raw_input('\nType a number from 1 to 20')

try:
x = int(x)
if x<1 or x>20: raise ValueError()
except ValueError:
Do_B
else:
Do_A


If you want to distinguish between the two error cases (not a number vs
number not in [1,20]), handle the second one as "Do_C" instead of
raising ValueError.

HTH,
George

Peter Maas

unread,
Sep 4, 2006, 4:02:54 PM9/4/06
to
Tim Williams wrote:
> On 04/09/06, Dr. Pastor <el...@adsihqx.com> wrote:
>> In the following code I would like to ascertain
>> that x has/is a number. What the simplest TEST should be?

def isnumber(value):
try:
value/1
return true
except TypeError:
return false

I think this works for all builtin types. Of course one could
overload '/' for a non-numeric class. The best solution would
be to introduce a super class base_number for int, long, float
and decimal, similar to base_string for str and unicode.

This would probably be not too hard to implement and surely not
break old code :)

Peter Maas, Aachen

Neil Cerutti

unread,
Sep 5, 2006, 10:29:20 AM9/5/06
to

Is the original value of x available in Do_B and Do_A, or will it
have been clobbered before getting there?

--
Neil Cerutti

George Sakkis

unread,
Sep 5, 2006, 1:43:32 PM9/5/06
to
Neil Cerutti wrote:

In Do_A, x will be an integer between 1 and 20. In Do_B, it depends; if
the original input cannot be converted to an int, it will be preserved,
otherwise x will be an integer (lower than 1 or larger than 20).

George

Neil Cerutti

unread,
Sep 5, 2006, 2:53:32 PM9/5/06
to

Thanks. I infer from this that in the case that int(x) raises
ValueError, that x is guaranteed to be unmodified.

--
Neil Cerutti
22 members were present at the church meeting held at the home
of Mrs. Marsha Crutchfield last evening. Mrs. Crutchfield and
Mrs. Rankin sang a duet, The Lord Knows Why. --Church Bulletin Blooper

0 new messages