0

So i'm trying to make simple hangman game, but while testing i cant append words from txt file to list, so the program can draw the word. Here is the code:

    import random
    words = []
    def wordsChoose(mode, words):
        mode = str(mode)+'.txt'
        for line in open(mode):
            words = words.append(line.rstrip())
        theword = random.choice(words)
        return theword

    wordsChoose('fruits', words)

i get the: 'AttributeError: 'NoneType' object has no attribute 'append''

maybe there is a better option to do this

1 Answers1

1

You should only call the .append method. You don't need to assign a variable to it -

import random
words = []
def wordsChoose(mode, words):
    mode = str(mode)+'.txt'
    for line in open(mode):
        words.append(line.rstrip())
    theword = random.choice(words)
    return theword

wordsChoose('fruits', words)

You get None Type Error, because .append is an in-place method (therefore, it returns None), so there is no point in assigning variables.

PCM
  • 2,692
  • 2
  • 7
  • 29