[Tutor] Python code trouble!

Dave Angel d at davea.name
Tue Nov 22 00:49:12 CET 2011


You're still posting from tomorrow.

On 11/22/2011 05:50 AM, John wrote:
> Hi all,
>
> I have attempted to create a programme which removes every Nth person 
> from the list until there is only one name remaining. N is inputted by 
> the user.
>
> Here is the code:
>
> def survivor(names, step):
>     next = names
>     while len(next) > 1:
>      index = step - 1
>      next.remove (next[index])
>         index = index + step - 1
>         while index > len(next):
>             index = index - len(next)
>         if index == len(next):
>             index = 0
>     return names[0]
>
Lots of things wrong with that code.  But first we need a complete 
description of what you want.  If you remove every nth item from a list, 
you'll end up with a somewhat shorter list, but not a single item.  For 
example, if you remove every 4th item from a 13 item list,  you'll end 
up with a 10-item list.

So presumably you're defining some kind of repeat, where you do it over 
and over.  But even then, if you try to remove every 4th item from a 
3-item list, you'll just end up with a 3-item list.

-- 

DaveA



More information about the Tutor mailing list