運行此程序時,我收到此錯誤消息:NameError: name 'wordList' is not defined。想不通為什么。謝謝。隨機導(dǎo)入定義主():def getRandomWord(wordList): wordIndex = random.randint(0, len(wordList) -1) return wordList[wordIndex]words = "Harry John Paul Jane Sue Frank Julie Tom Alice Sam".split()secretWord = getRandomWord(words)print("Welcome to You Bet Your Life with Groucho\n")print("Say the secret word and win a thousand dollars!")your_word = input("What is your guess for the secret word? ")if your_word == secretWord: print("Congratulations you are correct. The secrt word is", secretWord) print("and you win $1000 dollars !")else: print("Sorry, that is not the secret word.")playAgain = TruewordList = secretWordplayAgain = input("Do you want to play again? (yes or y) to continue")if playAgain == "Yes" or playAgain == "y": getRandomWord(wordList)else: print("Ok. Goodbye.")如果名稱== ' main ': main()
1 回答

RISEBY
TA貢獻1856條經(jīng)驗 獲得超5個贊
我看到你在重新開始游戲時遇到了麻煩。簡單地調(diào)用您的函數(shù)不會重新啟動整個游戲或?qū)⒛鷰Щ仨敳?,但您可以進行的一個小調(diào)整是將所有內(nèi)容放入while具有這樣條件的循環(huán)中
play = 'y'
while play == 'y':
words = "Harry John Paul Jane Sue Frank Julie Tom Alice Sam".split()
....
playAgain = input("Do you want to play again? (yes or y) to continue")
if playAgain == "Yes" or playAgain == "y":
play = 'y'
continue
else:
print("Ok. Goodbye.")
play = 'n'
所有這一切都是,如果play的值為 ,則y游戲?qū)⒅夭?,直到值更改?,n然后循環(huán)將結(jié)束,因此游戲
添加回答
舉報
0/150
提交
取消