1
import random
words=['left','none','super','bale']
i=4
luke=[]
nexa=[]
while i>0:
    new=random.choice(words)
    luke=luke.append(new)
    i=i-1
    for k in range(0,len(words)):
        if words[k]==new:
            nexa=words[0:k]+words[(k+1):4]
        else:
            continue
    words=nexa

print(luke)

I tried something like this but it is clearly not working any kind of help is really appreciated.

2 Answers2

2

You've over-complicated things. You can shuffle the list in-place and then iterate it, which guarantees that you can only print each word once, provided that you don't have duplicate words (in which case you could convert to a set() beforehand).

import random

words = ['a', 'b', 'c']

random.shuffle(words)

for word in words:
    print(word)

Because this works in place, you'll lose the initial ordering. If you need to retain the initial ordering then you can shuffle a copy of the list.

import random

words = ['a', 'b', 'c']
word_copy = words.copy()

random.shuffle(word_copy)

for word in word_copy:
    print(word)
roganjosh
  • 12,594
  • 4
  • 29
  • 46
0
import random
words = ['left', 'none', 'super', 'bale']
random.shuffle(words)
print(words)
Alexander C
  • 3,597
  • 1
  • 23
  • 39