newbie question on lists, getting an error on basic addition 
Author Message
 newbie question on lists, getting an error on basic addition

Quote:
>>> alist = [1, 2, 3, 4]
>>> counter = 0
>>> answer = 0
>>> alist
[1, 2, 3, 4]
>>> counter
0
>>> answer
0
>>> 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
Quote:
>>> answer

10

why am i getting this error when it works???
Rob



Fri, 17 Sep 2004 01:07:04 GMT  
 newbie question on lists, getting an error on basic addition


Quote:

>>>> alist = [1, 2, 3, 4]
>>>> counter = 0
>>>> answer = 0
>>>> alist
>[1, 2, 3, 4]
>>>> counter
>0
>>>> answer
>0
>>>> 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

Add a print counter at the top of your loop and you'll see why.
--

Why is this newsgroup different from all other newsgroups?



Fri, 17 Sep 2004 01:22:31 GMT  
 newbie question on lists, getting an error on basic addition
John Warney

Quote:
> >>> 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

---------



Fri, 17 Sep 2004 01:28:56 GMT  
 newbie question on lists, getting an error on basic addition
Ah, ok, thanks, it works fine now



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



Fri, 17 Sep 2004 05:49:56 GMT  
 newbie question on lists, getting an error on basic addition
Ah, ok, thanks, it works fine now


Quote:


>>>>> alist = [1, 2, 3, 4]
>>>>> counter = 0
>>>>> answer = 0
>>>>> alist
>>[1, 2, 3, 4]
>>>>> counter
>>0
>>>>> answer
>>0
>>>>> 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

>Add a print counter at the top of your loop and you'll see why.



Fri, 17 Sep 2004 05:49:47 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Addition Error and PACKAGE / TYPE questions

2. Newbie Question, list = list + x VS append (x)

3. Yer basic newbie smalltalk question (squeakish)

4. Clarion report writer-basic question (newbie)

5. NBSQ : newbie basic syntax question

6. newbie (very basic question)

7. newbie (basic question) part 2

8. Very Basic question from Newbie

9. basic newbie questions about SWI Prolog

10. Newbie Has Basic Question About XF

11. Extremely basic _Newbie_ question - help!?

12. question on event-generated errors (was Re: newbie TkCon question)

 

 
Powered by phpBB® Forum Software