[Tutor] IndexError: list index out of range

bhaaluu bhaaluu at gmail.com
Thu Sep 20 02:47:19 CEST 2007


On 9/19/07, Kent Johnson <kent37 at tds.net> wrote:
> bhaaluu wrote:
>
> > def popNum(num):
> >     if num == stackA[-1]:
> >         stackA.pop()
>
> What happens here if stackA is empty?
>
> Kent

This was how I was looking for the number.
It seems to me that if the list was EMPTY
or didn't have the sought after number,
it would look in the next list... after all, that's
what it did 16 times in the first round...
but now that I look back at it, stackA wasn't
empty until after the 5 had been moved out
of it. But the search worked even when stackB
and stackC were empty. Interesting. As far as
I could see, all three conditionals in popNum(num)
were the same, just checking different lists.

I tried Michael's suggestion and added:
if len(stackA) > 0 and num == stackA[-1]:
  etc.
to popNum(num) and that worked to continue
through the second and third rounds of test values.

Thank you very much!
-- 
bhaaluu at gmail dot com


More information about the Tutor mailing list