Python - Appending String To A List Nontype Error
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 wordlist=["chapman","machine","learning","computer","python","california", "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 correctword=wordlist[index] #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") print("*+_______________________________________________________+*\n") 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 == 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 else: 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 continue else: 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 print("*+_______________________________________________________+*\n") 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") else: 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
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module