Feb-25-2019, 09:13 PM
Dear friends,
I simply can't get my head around this little problem. I need to randomly choose three words out of a list of words. The chosen word cannot be a repetition and there has has to be exactly three - it is especially the last part that gets me. What I've done so far is this
Can you assist on this?
I simply can't get my head around this little problem. I need to randomly choose three words out of a list of words. The chosen word cannot be a repetition and there has has to be exactly three - it is especially the last part that gets me. What I've done so far is this
import random nouns = ["fossil", "horse", "aardvark", "judge", "chef", "mango", "extrovert", "gorilla"] new_lst = [] select_noun = random.choice(nouns) while select_noun not in new_lst : new_lst.append(select_noun) select_noun = random.choice(nouns) if len(new_lst) == 3: break print (new_lst)I get three words most of the times but as you can probably see it is open for making one or two-word lists as well.
Can you assist on this?