1

我正在尝试实施的 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)

就像免责声明一样,这不是我没有上学的意义上的作业。我只是一个试图重新开始编程并找到一些可以玩的任务的人。

提前致谢!

4

1 回答 1

1

看来我找到了答案。问题似乎与我对 letters_guessed 变量所做的分配有关。

而不是这样做:

letters_guessed = letters_guessed.append(userGuess)

我应该这样做:

letters_guessed.append(userGuess)
于 2013-02-03T20:29:38.210 回答