【发布时间】:2018-04-03 04:19:35
【问题描述】:
我正在编写一个刽子手游戏,我想将用户输入的字符添加到猜测列表中。 (忽略无错误检查)。然而,我仍然觉得这很烦人——“AttributeError: 'NoneType' object has no attribute 'append'”。
我的代码在 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)”行。它必须返回无,因为当我打印guessList 时,我什么也没有。但我没有做经典的guessList = guessList.append(letter)。此外,奇怪的是,它运行一次会打印guessList = None,然后是第二次(当它检查guessList 中的重复字符时它会崩溃。
【问题讨论】:
-
请给我们一个minimal reproducible example,它实际上重现了错误。如果我们假设
guessList = [],则没有错误。实际代码中的问题在于,您将guessList设置为None。这就是您需要修复的部分——而这部分不在您向我们展示的代码中,所以我们无法帮助您。 -
你从未定义过
guessList。所以你应该得到一个NameError。 -
不要使用全局变量。
find_letter应该有l作为参数。 -
@Daniel: How about any of these?
-
@l'L'l:看看 abarnert 的评论。这是一个问题,代码中没有显示。