NameError while using append on a list [Python]

10,607

Solution 1

You have the call backwards

append.wordlen("_")

You meant

wordlen.append("_")

Solution 2

The corrected code is

wordlen.append("_")

Another way to do it can be:

wordlen = ["_"]*len(word)

Or if you want just the string:

"_"*len(word)

Solution 3

You have given

 append.wordlen("_")

it should be

wordlen.append("_")

Solution 4

It should be - wordlen.append("_")

list objects have the append() function, not the otherway round.

Share:
10,607
Ciprum
Author by

Ciprum

Updated on June 05, 2022

Comments

  • Ciprum
    Ciprum almost 2 years

    I'm trying to make a simple hangman game in Python 2.7.10. However when I try to append "_" for every letter in the word in a list. It throws:

    Traceback (most recent call last):
    File "C:\Users\Janek\Dropbox\python\vjesalo.py", line 82, in <module>
        append.wordlen(olo)
    NameError: name 'append' is not defined
    

    No idea why this happends because if I run in the python terminal

    list = []
    list.append("Bla bla bla")
    

    It works just fine

    Here is my code:

    from sys import exit
    from time import sleep
    
            word = raw_input("Enter a word: ")
    
            if word.isalpha() == True:
                word.lower()
            else:
                print "Invalid word!"
                sleep(3)
                exit()
    
            wordlen = []    
            for i in range(len(word)):
                append.wordlen("_")
    
            print wordlen