我正在尝试用Python开发一个Hangman游戏,我一直在想为什么程序没有记住正确猜出的字母。你知道吗
例如,如果输入的字母在单词“hello”中,python会识别匹配项,但是当它提示输入另一个字母时,它会忘记updt_list
(包含输入字母的列表)。你知道吗
如果有人愿意看一眼,我会非常感激的。你知道吗
import time
def pick_a_word():
word = "hello"
return word
def guess_a_letter():
aletter = input("Please input one letter at a time:\n")
no_letters= len(aletter)
if no_letters > 1:
print("You inputed more than one letter! Don't forget- one at a time.\nLets try again..")
time.sleep(2)
return guess_a_letter()
else:
return aletter
def blankLIST(aword):
blankList = len(aword) * "_"
return list(blankList)
def check(aletter,aword,blankLIST):
updt_list= blankLIST(aword)
which_letter= aword.index(aletter)
updt_list[which_letter] = aletter
return updt_list
def engine():
guess=guess_a_letter()
wordtoguess=pick_a_word()
if guess in wordtoguess:
print("\n")
print("The letter is in the word")
print(check(guess,wordtoguess,blankLIST))
time.sleep(1)
else:
print("\n")
print("The letter is not in the word")
time.sleep(1)
def repeat():
x = 0
while x <= 5:
lifes= 6- x
print("You have",lifes," attempts left")
engine()
x = x
repeat()
请注意,这是非常不雅的,有问题,如果你继续猜测字母'l'在这种情况下。。。我没有更多的时间花在这上面,但它应该足以推动你一点前进。它现在记住了玩家的猜测,用户可以赢得游戏。我的道歉。。。你知道吗
相关问题 更多 >
编程相关推荐