0
我想在Python中開發一個Hang子手遊戲,並且我搞不明白爲什麼正確猜到的字母不被程序記住。Python的hang子手遊戲,沒有工作
例如,如果輸入的字母在單詞「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()
'忘記updt_list'它強行記住'check'中新創建的值,將被返回,打印和 - 忘記。 – greybeard