为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

达令说 2021-06-23 17:01:08
运行此程序时,我收到此错误消息: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 = 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条经验 获得超5个赞

我看到你在重新开始游戏时遇到了麻烦。简单地调用您的函数不会重新启动整个游戏或将您带回顶部,但您可以进行的一个小调整是将所有内容放入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然后循环将结束,因此游戏


查看完整回答
反对 回复 2021-06-29
  • 1 回答
  • 0 关注
  • 384 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号