newbie question on lists, getting an error on basic addition

John Warney john at yahoo.com
Sun Mar 31 16:49:56 EST 2002


Ah, ok, thanks, it works fine now

On Sun, 31 Mar 2002 17:28:56 GMT, "Emile van Sebille" <emile at fenx.com>
wrote:

>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.




More information about the Python-list mailing list