error with string (beginner)

Bruno Desthuilliers onurb at xiludom.gro
Mon Jun 26 05:04:36 EDT 2006


Jason wrote:
> I believe what you are trying to do is something like the following.
> 
> [code]
> def isIntLike(x):
> 	try:    int(x)
> 	except: return False

*Never* ever use a bare except clause. *Always* specify wich exceptions
you are expecting. (NB : here, TypeError and ValueError).

(NB :  of course, like for all and any do's-and-don't rules, there are
actually a very few cases where using a bare except may be ok.)

> 	else:   return True
> 
> something = raw_input("Enter something and I will tell you the type: ")
> 
> if isIntLike(something):	print "I am an int"

Nope. At this stage, 'something' is *not* an int - it's a string that
can be turned into an int.

> elif isinstance(something, type('')):	print "I am a string"

You don't need the "type('')" stuff - use isinstance(something,
basestring) instead.

> else:	print "I am an impostor!"
> [/code]

(snip)

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list