2 回答
TA贡献1802条经验 获得超5个赞
我会使用iter()
words=['cat', 'dog', 'mouse']
word=random.choice(words).lower()
lasthangman = hangmen[len(hangmen)-1]
hangmen = iter(hangmen)
attempts = 7
for i in range(attempts):
guess=input("Guess a letter: ")
if guess in word:
print('Yup, that letter is in the word.')
else:
try:
print(next(hangmen))
except StopIteration:
print(lasthangman)
每次猜错一个字母时,这将在hangmen 中打印下一个hangman 值,但如果我们用完了hangmen,它将继续打印最后一个hangman。请注意,这仍然是不完整的:如果我在for循环结束之前正确猜出了所有字母,会发生什么?
TA贡献1946条经验 获得超4个赞
您可以保留一个变量,只要用户错过了一个问题,就会添加一个变量,例如:
words=['cat', 'dog', 'mouse']
misses = 0
word=random.choice(words).lower()
attempts = 7
for i in range(attempts):
guess=input("Guess a letter: ")
if guess in word:
print('Yup, that letter is in the word.')
else:
print('Incorrect.')
misses += 1
print(hangmen[misses])
这将打印列表中具有变量未命中索引的项目
添加回答
举报