为什么我的函数返回错误消息:'wordlist is not defined

运行此程序时,我收到此错误消息:NameError: name 'wordList' is not defined。想不通为什么。谢谢。


随机导入


定义主():


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 = True

wordList = secretWord

playAgain = 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()


达令说
浏览 385回答 1
1回答

RISEBY

我看到你在重新开始游戏时遇到了麻烦。简单地调用您的函数不会重新启动整个游戏或将您带回顶部,但您可以进行的一个小调整是将所有内容放入while具有这样条件的循环中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游戏将重播,直到值更改为 ,n然后循环将结束,因此游戏
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python