我正在编写一个刽子手游戏,我想将用户输入的字符添加到猜测列表中。 (忽略无错误检查)。然而,我继续让这很烦人 - "属性错误:' NoneType'对象没有属性'追加' &#34 ;.
我的代码在while循环中抛出guessList []上的错误。
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[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
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")
它指向" guessList.append(letter)"行。它必须返回none,因为当我打印guessList时,我没有。但我没有做经典的guessList = guessList.append(字母)。另外,奇怪的是,它会运行一次打印guessList = None,然后第二次(当它检查guessList中的字符重复时它会崩溃。
答案 0 :(得分:0)
根据@Daniel对你的问题的评论。如果你没有声明变量,你将获得NameError
。据我说,你已经宣布guessList
为全局变量。有时python无法正确识别global variables
。因此,您需要使用global
关键字在函数中指定它是全局变量。
所以你的代码应该在这个函数中有global guessList
def main():
global guessList
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)
如果不是这种情况,那么唯一可能的错误可能是以下任何一种情况。
guessList
初始化为None
。我重新分配为guessList=[]
。from module import guessList
或 import module
并用作module.guessList
。 guessList=[]
。答案 1 :(得分:0)
使用整个代码,错误很明显。你没有经典的追加错误,但同样的错误:
guessList = guessList.sort()
sort
对列表进行排序并返回None
。