[Tutor] repeat a sequence in range

Dave Angel d at davea.name
Sun Feb 12 00:31:15 CET 2012

On 02/11/2012 06:22 PM, Michael Lewis wrote:
> I am trying to repeat a certain sequence in a range if a certain even
> occurs. Forgive me for not pasting my code; but I am not at the machine
> where it's saved.
> Basically, I want to get user input and append that input to a list only if
> the input is not already in the list. I want to do this x amount of times,
> but if the user input is already in the list, then I want to repeat that
> step in range(x).
> At the prompt, I want to ask the user for:
> Name 1:
> Name 2:
> Name 3:
> etc....
>   but if the user input for Name 3 was the same as the user input for Name
> 2, then I want to ask the user again for Name 3 instead of continuing to
> Name 4.
> How can I do this?

You imply that you're using a range, and I suspect you're writing the 
loop as
      for elem in range(10):

But since you don't know how many times you want to loop, why not switch 
to a different loop construct?

while len(mylist) < 10:

then you do your input and if the answer is not in the list, append it 
and loop around.  If it is in the list, tell the user that it's a 
duplicate and loop around without appending.



More information about the Tutor mailing list