如何用PYTHON制作猜词语游戏

2025-12-20 01:57:21

1、打开JUPYTER NOTEBOOK,新建一个空白的PY文档。

如何用PYTHON制作猜词语游戏

2、guess_this_word = "happy"

guess = ""

首先要定义两个变量。

如何用PYTHON制作猜词语游戏

3、guess_this_word = "happy"

guess = ""

while guess != guess_this_word:

    guess = input("Please guess the word: ")

print("You guess the word!")

用WHILE就可以不断地询问让用户输入。

如何用PYTHON制作猜词语游戏

4、但是实际上一般的游戏并不想让用户有无限的机会。

如何用PYTHON制作猜词语游戏

5、guess_this_word = "happy"

guess = ""

count = 0

while guess != guess_this_word and count < 3:

    guess = input("Please guess the word: ")

    count = count + 1

print("You guess the word!")

一般新手会犯这样的错误,直接把条件全部输入。这样条件就直接回打印语句出来了。

如何用PYTHON制作猜词语游戏

6、guess_this_word = "happy"

guess = ""

count = 0

count_limit = 3

while guess != guess_this_word:

    if count < count_limit:

        guess = input("Please guess the word: ")

        count += 1

    else:

        print("no more chance")

print("You guess the word!")

也有人会犯这样的错误,要记住WHILE是会不断执行。

如何用PYTHON制作猜词语游戏

7、guess_this_word = "happy"

guess = ""

count = 0

count_limit = 3

no_chance = False

while guess != guess_this_word:

    if count < count_limit:

        guess = input("Please guess the word: ")

        count += 1

    else:

        no_chance = True

print("You guess the word!")

当我们增加TRUE和FALSE条件的时候就可以中断WHILE 了。

如何用PYTHON制作猜词语游戏

8、尝试一下其它结果,猜中就会有提示。这样就正确了。

如何用PYTHON制作猜词语游戏

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢