sys.argv and while loop
Peter Hansen
peter at engcorp.com
Thu May 9 00:58:22 EDT 2002
Christopher Myers wrote:
>
> try:
> num=string.atoi(str)
> except ValueError:
> try:
> num=string.atof(str)
> except:
> num=str
>
> if type(num) == type(1.0):
> print "Do float stuff in here"
> elif type(num) == type(1):
> print "Do int stuff in here"
> else:
> print "Value does not convert to a number"
>
> I'd be very interested to see if there's a simpler way, though.
Well, this would probably be as useful in most situations,
since the need to differentiate types that carefully is probably
rare:
try:
num = float(str)
print 'Do generic number stuff here'
except ValueError:
print "Value not a number"
Obviously you know you could do that, since you were
able to come up with the more complex version above.
My point is merely that it is unlikely anything so
complex would often be necessary, so the "simpler way"
is perhaps just this little version.
-Peter
More information about the Python-list
mailing list