----== 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 =----
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 :)
One step further
try:
eval(x+'0')
....
--
Helmut Jarausch
Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
> 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
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.
> 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
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
Is the original value of x available in Do_B and Do_A, or will it
have been clobbered before getting there?
--
Neil Cerutti
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
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