我正在为EdX MIT Python课程进行这项作业,并决定我希望输出以不同的方式显示。根据我的代码,我认为任务程序将在猜测= 0时结束。但是,我要么收到“ IndexError:列表分配索引超出范围”,要么程序在猜测2结束。这似乎取决于长度的秘密字。谁能指出我正确的方向?
def hangman(secretWord):
'''
secretWord: string, the secret word to guess.
Starts up an interactive game of Hangman.
* At the start of the game, let the user know how many
letters the secretWord contains.
* Ask the user to supply one guess (i.e. letter) per round.
* The user should receive feedback immediately after each guess
about whether their guess appears in the computers word.
* After each round, you should also display to the user the
partially guessed word so far, as well as letters that the
user has not yet guessed.
Follows the other limitations detailed in the problem write-up.
'''
trackedguess = []
letcount = ()
letterlist = []
guess = ''
for i in range(0, (len(secretWord)-1)):
trackedguess.append('_')
letcount = len(secretWord)
guessesleft = 8
for i in range(0, 7):
if ''.join(trackedguess) == secretWord:
print('You win!')
break
if guessesleft < 1:
print('You have 0 guesses remaining.')
break
print(trackedguess)
print("You have ", guessesleft, " guesses remaining.")
guess = input('Please guess a letter and press return: ')
if guess in letterlist:
print("You've already guessed that. Try again.")
else:
guessesleft -= 1
letterlist.append(guess)
for i in range(0, len(secretWord)):
if secretWord[i] in letterlist:
coordinate = i
trackedguess[coordinate] = secretWord[i]
hangman(chooseWord(wordlist))
答案 0 :(得分:0)
有一些事情要向我展示:
range
使用排他末尾。这意味着range(0, (len(secretWord)-1)
将比secretWord
的长度少 次。您希望长度匹配。更直接,更不易出错的方法就是:trackedGuess = ['_'] * len(secretWord)
或trackedGuess = list('_' * len(secretWord))
。
您应该验证您的假设。例如,如果您进行了assert(len(trackedGuess) == len(secretWord))
,可能很容易发现上述情况。
for i in range(0, 7)
与您先前使用range()
时遇到相同的问题。如果要迭代8(guessesleft
)次,则应使用range(0, 8)
。但是,您还将在循环内递减guessesleft
并在其达到0时退出。执行一个或另一个,但不要同时执行。就像您当前的代码一样,如果有人输入他们已经做出的猜测,它将对他们进行一次迭代(我不确定您想要什么)。
答案 1 :(得分:0)
这是一个很好的尝试,但是有些错误。我将尝试一一介绍。
建议不要使用for
循环,而应该使用while guessesleft>0
。在当前实现中,无论是否存在任何猜测,for循环将运行8次(例如,每次尝试提供与猜测相同的字母)。但是,借助while
,您可以更好地控制循环。
trackedguess
一代有缺陷。它总是会丢失secretword
的最后一个字母(这也是您获得IndexError
的原因)请尝试使用for i in range(len(secretWord))
。您还将发现它更加简洁易读。
我还自由地将获胜或失败的条件降低了。以前,如果您在最后的猜测中获胜,您仍然会输(因为获胜检查条件发生在输入之前和循环结束之后);另外,如果您赢了,也不会打印出猜测(因为循环会在打印语句之前中断)。
下面的修订代码:
def hangman(secretWord):
'''
secretWord: string, the secret word to guess.
Starts up an interactive game of Hangman.
* At the start of the game, let the user know how many
letters the secretWord contains.
* Ask the user to supply one guess (i.e. letter) per round.
* The user should receive feedback immediately after each guess
about whether their guess appears in the computers word.
* After each round, you should also display to the user the
partially guessed word so far, as well as letters that the
user has not yet guessed.
Follows the other limitations detailed in the problem write-up.
'''
trackedguess = []
letterlist = []
for i in range(len(secretWord)):
trackedguess.append('_')
guessesleft = 8
while guessesleft > 0:
print(trackedguess)
print("You have ", guessesleft, " guesses remaining.")
guess = input('Please guess a letter and press return: ')
if guess in letterlist:
print("You've already guessed that. Try again.")
else:
guessesleft -= 1
letterlist.append(guess)
for i in range(0, len(secretWord)):
if secretWord[i] in letterlist:
coordinate = i
trackedguess[coordinate] = secretWord[i]
if ''.join(trackedguess) == secretWord:
print(trackedguess)
print('You win!')
break
if guessesleft < 1:
print('You have 0 guesses remaining.')
break
hangman('test')
希望有帮助。