Reputation: 738
So I have this list:
words = ["Games","Development","Keyboard","Speed","Typer","Anything","Alpha","Zealous","Accurate","Basics","Shortcut","Purpose","Window","Counter","Fortress","Modification","Computer","Science","History","Football","Basketball","Solid","Phantom","Battlefield","Advanced","Warfare","Download","Upload","Antidisestablishmentarianism","Supercalifragilisticexpialidocious","Discombobulation","Liberated","Assassin","Brotherhood","Revelation","Unity","Syndicate","Victory"]
And I have this which shuffles it and displays it on a label:
entry.delete(0, tkinter.END)
random.shuffle(words)
label.config(text=str(words[1]))
timeLabel.config(text="Time: " + str(time_score)+ "s")
I'd like to know how to make this shuffle truly random because it feels like I'm getting the same word over and over again despite there being a lot in the list. Maybe there is a way to remove a value from the list once it's been taken from it so it can't be shown again?
Upvotes: 2
Views: 222
Reputation: 655
You can try:
import random
random_word = random.choice(words)
to choose random word from your array. Then you can do:
words.remove(random_word)
to remove this randomly selected word from array to avoid getting it again if you want to get next random word.
Edit - answer to the comment
When I run this code below I'm getting different word every time. Is this the logic you are looking for?
words = ["Games","Development","Keyboard","Speed","Typer","Anything","Alpha","Zealous","Accurate","Basics","Shortcut","Purpose","Window","Counter","Fortress","Modification","Computer","Science","History","Football","Basketball","Solid","Phantom","Battlefield","Advanced","Warfare","Download","Upload","Antidisestablishmentarianism","Supercalifragilisticexpialidocious","Discombobulation","Liberated","Assassin","Brotherhood","Revelation","Unity","Syndicate","Victory"]
import random
while words:
random_word = random.choice(words)
print(random_word)
words.remove(random_word)
Upvotes: 3
Reputation: 2236
A variation @grael's solution is to shuffle, and then use list.pop
to remove items. There's no need to shuffle your list more than once.
In [54]: words = ['foo','bar','baz','quux','bizby','squiggle']
In [55]: random.shuffle(words)
In [56]: while words: print(words.pop())
bizby
bar
foo
baz
squiggle
quux
Upvotes: 4