2012-11-24 80 views
0

好吧,我正在做一個家庭作業,以在python中建立一個hang子手遊戲。到目前爲止,這是很順利,直到我得到這個惱人的錯誤:Hangman蟒蛇遊戲索引錯誤在For循環與列表

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" 
+0

FWIW,蟒蛇不需要對控制結構括號 - '如果( remGuesses == 0):'如果remGuesses == 0的話比python更少'' – Eric

+0

因爲沒有人提到過 - '.split()'使用'string'模塊有什麼用處 - 這已經不合時宜了 - 而且我沒有看到任何優勢打開wordlist作爲無緩衝... –

回答

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 
+0

你的第二個想法沒有奏效,我根本沒有得到第一個邏輯 – user1624992

+0

@ user1624992 - 我猜它扔了一個ValueError,因爲userGuess不在'remLetters'中。 – mgilson

+0

第一個工作雖然 – user1624992

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:] 
+0

但我將如何使用.index() – user1624992

+0

如何使用.index雖然 – user1624992