我在看看蟒蛇,但我似乎已經遇到了一個問題:的Python:Hangmangame IndexError:列表分配索引超出範圍
我有兩個數組,一個是字符串的每一封信我隨機得到從一個文件和另一個用'_'填充的單詞中的每個字母,用戶需要找到。
但是,似乎我的while循環並沒有結束,即使這個詞被發現它繼續前進。
任何人都可以找到我的錯誤嗎?
這是我在Python第一個代碼,我來自Java的等如何編寫更好的技巧和海因茲也讚賞:)
感謝。
我的代碼:
'''
Created on 12-jun.-2011
@author: k3r3nks7
'''
import random
class Hangman(object):
def _init_(self):
self.fileInput()
self.printStuff()
def fileInput(self):
wordList = open('wordlist')
woordenArray=[]
for line in wordList:
woordenArray.append(line)
i = random.randrange(0,len(woordenArray))
self.randomWoord = woordenArray[i]
def printStuff(self):
print 'I want to play a game\n\n'
print 'Hangman spelen ? (y/n)\n'
stringEval = raw_input()
self.checkToPlay(stringEval)
def checkToPlay(self,woord):
if woord == 'y':
print '#####################'
print '#let the game begin!#'
print '#####################'
self.iwanttoplayagame()
elif woord == 'n':
print 'have a nice day'
exit()
else:
print 'Your answer was not correct, please answer a correct parameter,\n resistance is futile, \n We are Borg sheep and .... \n ... oh look fresh gras \n'
self.printStuff()
def iwanttoplayagame(self):
incorrectGuesses = 0
self.correctArray = []
for char in self.randomWoord:
self.correctArray.append('_')
test=""
while(incorrectGuesses <= 6 or self.correctArray != self.randomWoord):
print 'u raadde al : '+test.join(self.correctArray)
print "Geef is een letter \n"
letterInput = raw_input()
_c=0
if letterInput in self.randomWoord:
for letter in self.randomWoord:
if(letter == letterInput):
self.moveChar(letter,_c)
_c+=1
else:
incorrectGuesses+=1
self.display_figure(incorrectGuesses)
def moveChar(self,letter,c):
self.correctArray[c] = letter
def display_figure(self,bad_guesses):
graphics = [
"""
+--------+
|
|
|
|
|
|
====================
""",
"""
+-------
| o
|
|
|
|
====================
""",
"""
+-------
| o
| ---+---
|
|
|
======================
""",
"""
+-------
| o
| ---+---
| |
|
|
|
=====================
""",
"""
+-------
|
| o
| ---+
| |
| /
| /
|
|
|
|
=====================
""",
"""
+-------
| o
| ---+---
|
| /
| /
| /
|
=====================
""",
"""
+-------
| o
| ---+---
|
| /\
| / \
| / \
|
=====================
"""
,]
if(bad_guesses== 7):
print "you lost the game. The correct answer was : "+self.randomWoord
print "\n Do you want to play again ?"
inputS = raw_input()
self.checkToPlay(inputS)
print graphics[bad_guesses]
f = Hangman()
f._init_()
y這解決了一切:D – 2011-06-12 19:30:28
我看到你在我之前得到這個,+1 :) – mouad 2011-06-12 19:36:41