[Tutor] parsing woes

Vicki Stanfield vicki at thepenguin.org
Sun Aug 24 09:28:32 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.
This is what I am currently doing. It seems to be working fine.

>
> 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!"
>
I'll have to look into the len function. I was under the assumption that
not knowing what the length of the parameter would prohibit its use. The
parameters could be a single letter or number of multiple letter/number
combination. I only know that they will be space-delimited.

--vicki



More information about the Tutor mailing list