0

好的,我正在做一个家庭作业,用 python 构建一个刽子手游戏。到目前为止,一切都很顺利,直到我收到这个烦人的错误:

Traceback (most recent call last):  
  File "/Users/Toly/Downloads/ps2 6/ps2_hangman.py", line 82, in <module>
    if (remLetters[i] == userGuess):
IndexError: string index out of range 

这是我的代码:

    # 6.00 Problem Set 3
    # 
    # Hangman
    #


    # -----------------------------------
    # Helper code
    # (you don't need to understand this helper code)
    import random
    import string
    import time

    WORDLIST_FILENAME = "words.txt"

    def load_words():
        print "Loading word list from file..."
        # inFile: file
        inFile = open(WORDLIST_FILENAME, 'r', 0)
        # line: string
        line = inFile.readline()
        # wordlist: list of strings
        wordlist = string.split(line)
        print "  ", len(wordlist), "words loaded."
        return wordlist

    def choose_word(wordlist):
        """
        wordlist (list): list of words (strings)

        Returns a word from wordlist at random
        """
        return random.choice(wordlist)

    wordlist = load_words()

    blankword="_ "
    word=random.choice (wordlist)    
    remLetters = string.lowercase
    remGuesses = 8 #starting number of guesses
    remWord=len(word)

    #makes a blank with the length of the word
    print "Welcome to Hangman"
    time.sleep(1)
    print
    print "Your word is", remWord,"letters long."
    print
    time.sleep (1)

    while (remGuesses != 0 or blankword != word):
        remBlankword=len(blankword)


 remWordDoubled=remWord*2
    while (remWordDoubled!=len(blankword)):
        blankword=blankword + "_ "
    print blankword
    print
    print "You have",remGuesses," guesses left."
    print
    time.sleep(1)
    userGuess= str(raw_input ("Guess a letter:"))
    print
    if (userGuess in word):
        print "Excellent guess!"

    else:
       print "Bad Guess"
       remGuesses=remGuesses-1
    for i in range (1, len(remLetters)):
       if (remLetters[i] == userGuess):
            remLetters = remLetters[0:i] + remLetters[i+1:len(remLetters)]

    print remLetters


if (remGuesses == 0):
    print
    print "Sorry, you died! Ha, sucks!"
    print
    print
    time.sleep (1)
    print "End of Game"


if (blankword == word):
    print
    print "Congradulations! You won!"
    print
    time.sleep(1)
    print 
    print
    print
    print "End of Game"
4

2 回答 2

2

您首先获得 中的索引范围remLetters,但如果字母 == userGuess,则从 中删除一个字母remLetters。这意味着现在最大的指数remLetters比以前少了 1。当您尝试索引您的范围返回的最高数字时,您现在超出范围,因此您得到一个IndexError.

像这样的东西可能是你正在寻找的东西:

remLetters = ''.join(x for x in remLetters if x != userGuess)

或者:

try:
    idx = remLetters.index(userGuess)
    remLetters = remLetters[:idx] + remLetters[idx+1:]
except ValueError:
    pass
于 2012-11-24T15:43:40.617 回答
1

您正在更改remLetters循环中的字符串,使其更短。所以你最终得到一个超出长度的索引remLetters

for i in range (1, len(remLetters)):
   if (remLetters[i] == userGuess):
        remLetters = remLetters[0:i] + remLetters[i+1:len(remLetters)]

改为使用.index()来查找匹配项:

while userGuess in remLetters:
    i = remLetters.index(userGuess)
    remLetters = remLeters[:i] + remLetters[i+1:]
于 2012-11-24T15:43:51.517 回答