Python - Appending String To A List Nontype Error

- 1 answer

I am writing a hangman game, and I want to add the user inputted char to the list of guesses. (Ignore the no error checking). I, however, continue to get this annoying - "AttributeError: 'NoneType' object has no attribute 'append' ".

My code that throws the error on guessList[] within the while loop.

import random

      "jellybeans","coffee","laboratory","disneyland","library", "freedom",
      "happiness", "majority", "vexing", "undulation", "periphery", "exultant",
      "jeering", "trampoline","weirdo","blondie","bowtie","controller","completion"]
index=random.randint(0, len(wordlist) - 1) #selects a random integer from range of list
#declaring variables
maxGuesses = len(correctword) + 5
guessCounter = 0
sofar= []
win = False 
wordLetters = 0
whileloop = False
mainLoop = True
guessList = []

for char in correctword:
    sofar.append("_") #makes the list have only Dashes
    wordLetters = wordLetters + 1
#display the length of wordlist (done in loops)
# print(sofar)
# print(" ".join(sofar)) #joins the elements in sofar into a string

print("Hangman game! Let's begin")

def findInd(string, char):
    return [i for i, letter in enumerate(string) if letter == char]
    #This will be called to find all indexes of letter in CorrectWord. 

def find_letter(list):
    if not list:          
        return 0

    elif list[0] == l:  #check first element here for guessed letter
        return True

    elif find_letter(list[1:]):  # checked the first element, skip it and return the other elements of the list
        return True

        return False

while (mainLoop == True):
    print ('The word to guess: ',  " ".join(sofar), wordLetters, "letters long")
    letter = raw_input("guess a letter: ")
    guessCounter = guessCounter +1

    l = letter
    if (find_letter(guessList) == True):
        print("You already guessed that letter!")
        guessCounter = guessCounter -1
        guessList.append(letter) #Here is where it declares "NonType Error"

    for char in correctword:
        if letter == char:
            print ("good guess")
            whileloop = True
            mainLoop = True

    if whileloop == False:
        print ("That letter is not in the word! Guess again. \n")
        mainLoop = True

    #will skip this loop if letter is wrong
    while (whileloop == True):
        for num in (findInd(correctword, letter)): #returns list of indexes
            sofar[num] = letter #Replaces the places in sofar with char
        whileloop = False;  
            #if (letter == char): 
            #index = correctword.find(letter)
            #sofar[index] = letter


    count = sofar.count("_")

    if count == 0:
        win = True
        break #exits the main while loop

    guessList = guessList.sort()
    print ("you have guessed these letters: ", guessList)
    print ("Guesses left: ", (maxGuesses-guessCounter))
    if guessCounter == maxGuesses:
        break #User has used all the guesses and not won.

#outside of While loop

if win == True:
    print ("CONGRADULATIONS! You won. The word was ", correctword)
    print (" Thanks for playing")
    print ("heh you lose... Try again some time")

It points to the line "guessList.append(letter)". It must return none because when I print guessList, I get none. But I'm not doing the classic guessList = guessList.append(letter). Also, oddly enough, it runs once where it will print guessList = None, and then the second time (when it checks for repetition of character in guessList it crashes.



With your whole code, the error is clear. You don't have the classic append error, but the same mistake:

guessList = guessList.sort()

sort sorts the list inplace and returns None.