如何用PYTHON制作猜词语游戏

 时间:2026-02-16 03:30:29

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制作猜词语游戏

  • 怎样才能延迟开学
  • win10笔记本文件更改为Java后缀扩展名
  • FGO库·丘林【Alter】 黑狗满破图鉴
  • 植物呼吸加剧温室效应?碳影响植物对气候变化的反应
  • APMServ5.2.6如何创建数据库
  • 热门搜索
    闫芳最后怎么处理的 逝怎么读 绿萝叶子发黄怎么办 怎么补钙 土豆怎么种 苹果怎么查激活时间 headache怎么读 回旋镖怎么折 孕妇胃胀气怎么办 头皮痒怎么办