dimanche 3 janvier 2016

importing random words from a file without duplicates Python

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 list for this however the same words seem to appear. 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!

please find relevant code below! -(p.s. FileSelection is basically open file dialog)

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




Aucun commentaire:

Enregistrer un commentaire