How to test if a variable has a natural number?
Piet van Oostrum
piet at cs.uu.nl
Sat Jun 21 08:03:06 EDT 2003
>>>>> pythonguy at Hotpop.com (Anand Pillai) (AP) wrote:
AP> You can still use types. Just add a check for LongTypes also.
AP> from types import *
AP> def natural(num):
AP> if num>=0 and (type(num) is IntType or type(num) is LongType):
AP> return 1
AP> else:
AP> return 0
It is better to do the type test first.
Otherwise:
>>> natural(1+1j)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in natural
TypeError: cannot compare complex numbers using <, <=, >, >=
--
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.van.Oostrum at hccnet.nl
More information about the Python-list
mailing list