[Tutor] IndexError: list index out of range

Alan Gauld alan.gauld at btinternet.com
Mon Nov 21 20:00:18 CET 2011

On 22/11/11 05:28, John wrote:

>>>> def survivor(names, step):
> index = step - 1
> next = names
> while len(next)> 1:
> next.remove (next[index])
> However when ever i run it i get this error message:
> next.remove (next[index])
> IndexError: list index out of range
> Any ideas about whats causing this error?

You are doing the programming equivalent of sitting on the branch of a 
tree and cutting 2 feet off the end of the branch. But you never move 
your position so eventually you cut off the section of branch you are 
sitting on...

You never change index so eventually you remove enough of next that 
theres no longer any data at index.

Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list