如何用PYTHON制作猜词语游戏
1、打开JUPYTER NOTEBOOK,新建一个空白的PY文档。
2、guess_this_word = "happy"guess = ""首先要定义两个变量。
3、guess_this_word = "happy"guess = ""while guess != guess_this_word: guess = input("Please guess the word: ")print("You guess the word!")用WHILE就可以不断地询问让用户输入。
4、但是实际上一般的游戏并不想让用户有无限的机会。
5、guess_this_word = "happy"guess = ""count = 0while guess != guess_this_word and count < 3: guess = input("Please guess the word: ") count = count + 1print("You guess the word!")一般新手会犯这样的错误,直接把条件全部输入。这样条件就直接回打印语句出来了。
6、guess_this_word = "happy"guess = ""count = 0count_limit = 3while 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是会不断执行。
7、guess_this_word = "happy"guess = ""count = 0count_limit = 3no_chance = Falsewhile guess != guess_this_word: if count < count_limit: guess = input("Please guess the word: ") count += 1 else: no_chance = Trueprint("You guess the word!")当我们增加TRUE和FALSE条件的时候就可以中断WHILE 了。
8、尝试一下其它结果,猜中就会有提示。这样就正确了。