我正在尝试实施的OpenCourseWare项目存在问题。我正在创建一个刽子手游戏,一切似乎都没问题,直到我尝试运行游戏的实际功能。
我收到以下错误:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "Game of Hangman/hangman.py", line 118, in play_hangman
print(print_guessed())
File "Game of Hangman/hangman.py", line 92, in print_guessed
if letter in letters_guessed == True:
TypeError: argument of type 'NoneType' is not iterable
我似乎无法理解为什么列表正在评估NoneType,即使它被声明为空列表。我已经使用控制台尝试找到答案,并说类型是NoneType。有人可以帮帮我吗?我已经提供了代码作为参考。
# Name:
# Section:
# 6.189 Project 1: Hangman template
# hangman_template.py
# Import statements: DO NOT delete these! DO NOT write code above this!
from random import randrange
from string import *
# -----------------------------------
# Helper code
# (you don't need to understand this helper code)
# Import hangman words
WORDLIST_FILENAME = "words.txt"
def load_words():
"""
Returns a list of valid words. Words are strings of lowercase letters.
Depending on the size of the word list, this function may
take a while to finish.
"""
print "Loading word list from file..."
# inFile: file
inFile = open(WORDLIST_FILENAME, 'r', 0)
# line: string
line = inFile.readline()
# wordlist: list of strings
wordlist = split(line)
print " ", len(wordlist), "words loaded."
print 'Enter play_hangman() to play a game of hangman!'
return wordlist
# actually load the dictionary of words and point to it with
# the words_dict variable so that it can be accessed from anywhere
# in the program
words_dict = load_words()
# Run get_word() within your program to generate a random secret word
# by using a line like this within your program:
# secret_word = get_word()
def get_word():
"""
Returns a random word from the word list
"""
word=words_dict[randrange(0,len(words_dict))]
return word
# end of helper code
# -----------------------------------
# CONSTANTS
MAX_GUESSES = 6
# GLOBAL VARIABLES
secret_word = get_word()
letters_guessed = []
# From part 3b:
def word_guessed():
'''
Returns True if the player has successfully guessed the word,
and False otherwise.
'''
global secret_word
global letters_guessed
wordGuessed = False
####### YOUR CODE HERE ######
for letter in secret_word:
if letter in letters_guessed:
wordGuessed = True
else:
wordGuessed = False
break
return wordGuessed
def print_guessed():
'''
Prints out the characters you have guessed in the secret word so far
'''
global secret_word
global letters_guessed
printedWord = []
####### YOUR CODE HERE ######
for letter in secret_word:
if letter in letters_guessed:
printedWord.append(letter)
else:
printedWord.append("-")
return printedWord
def play_hangman():
# Actually play the hangman game
global secret_word
global letters_guessed
# Put the mistakes_made variable here, since you'll only use it in this function
mistakes_made = 0
# Update secret_word. Don't uncomment this line until you get to Step 8.
secret_word = get_word()
####### YOUR CODE HERE ######
while mistakes_made < MAX_GUESSES or word_guessed() == False:
print("WORD:")
userGuess = raw_input("What is your guess?\n").lower()
if userGuess in secret_word:
letters_guessed = letters_guessed.append(userGuess)
else:
letters_guessed = letters_guessed.append(userGuess)
mistakes_made += 1
print(print_guessed())
if word_guessed() == False:
print("Sorry but you've run out of guesses...")
else:
print("You've correctly guessed the secret word!")
print("Secret Word: " + secret_word)
就像免责声明一样,这不是我未入学的意义上的任务。我只是一个想要重新编程并找到一些作业的人。
提前致谢!
答案 0 :(得分:1)
看起来我找到了答案。问题似乎在于我使用letters_guessed变量进行的赋值。
而不是:
letters_guessed = letters_guessed.append(userGuess)
我应该做的:
letters_guessed.append(userGuess)