February 21st, 2006, 03:26 PM
Newbie question (loops)
I have to make here a program that prints a list of words in random order. and the words cannot repeat.
i have this
But i don't know whty it doesn't work. The loop works but some words repeat, even though i remove them from the list.
words = ["one", "two", "three", "four", "five"]
for i in range(len(words)):
words2 = words[:]
randitem = random.choice(words2)
February 21st, 2006, 03:50 PM
Your copying words each time though the loop then removing the random word from that copy .
Originally Posted by play_
You could remove the line defining words2 or move it outside the loop, then just make sure you're removing from the appropriate list and your set.
You might also want to try random.shuffle() then you can just print them normally by looping though.
February 21st, 2006, 03:58 PM
haha yea, i got fed up and checked the python website for other functions and tried out shuffle.
Originally Posted by netytan
but the book hasnt showed shuffle yet, so i wanna do it with what the book has shown. Ill try out your advice.
and yes, it worked. thanks again