lundi 4 janvier 2016

Choosing random words from a file without duplicates Python (sets)

I'm attempting to create a program which selects 10 words from a text file which contains 10+ words. For the purpose of the program when importing these 10 words from the text file, I must not import the same words twice! Currently I'm utilising a set for this however I'm greeted by a syntax error. I have some knowledge of sets and know they cannot hold the same value twice. As of now I'm clueless on how to solve this any help would be much appreciated. THANKS!

Relevent Code: (FileSelection)= open file dialog

def GameStage03_E():
global WordSet
if WrdCount >= 10:
    WordSet = set()
    for n in range(0,10):
        FileLines = open(FileSelection).read().splitlines()
        RandWrd = random.choice(FileLines)
        WordSet.update(set([RandWrd]))
    SelectButton.destroy()
    GameStage01Button.destroy()
    GameStage04_E()
elif WrdCount <= 10:
    tkinter.messagebox.showinfo("ERROR", " Insufficient Amount Of Words Within Your Text File! ")

error code:

  File "C:\Python34\lib\random.py", line 256, in choice
return seq[i]

TypeError: 'set' object does not support indexing




Aucun commentaire:

Enregistrer un commentaire