newbie question on lists, getting an error on basic addition

Emile van Sebille emile at fenx.com
Sun Mar 31 12:28:56 EST 2002


John Warney
> >>> alist = [1, 2, 3, 4]
> >>> counter = 0
> >>> answer = 0
> >>> alist
[snip]
> >>> while counter < 6:
> ... answer = answer + alist[counter]
> ... counter = counter + 1
> ...
> Traceback (most recent call last):
>   File "<interactive input>", line 2, in ?
> IndexError: list index out of range
> >>> answer
> 10
>
> why am i getting this error when it works???
> Rob

You get the error because you're trying to loop six times through a list
of four elements.  What you've displayed is the accumulated total while
the loop worked.

--

Emile van Sebille
emile at fenx.com

---------




More information about the Python-list mailing list