[Tutor] parsing woes
Alan Gauld
alan.gauld at blueyonder.co.uk
Sun Aug 24 09:13:52 EDT 2003
> when I try to run this code to see what the values are:
> if parameters[0]:
> print parameters[0]
> if parameters[1]:
> print parameters[1]
> if parameters[2]:
> print parameters[2]
There are at least two approaches you could use:
try:
if ....
except IndexError:
pass
That will simply ignore the index error.
Or more explicitly (prevention is better than cure philosophy)
num = len(parameters)
if num == 0: print "No parameters"
if num > 0: print parameters[0]
if num > 1: print parameters[1]
if num > 2: print parameters[2]
if num > 3: print "Too many parameters!"
HTH,
Alan G.
More information about the Tutor
mailing list