这个 python 程序应该检查匹配的字符串,但是在三个错误的输入之后,编程没有终止

// 匹配字符串程序


exact_word = "Guessing"

    guess_kid = ""

    guess_count = 0

    guess_limit = 3

    out_of_guesses = False


    while guess_kid != exact_word.casefold() and not(out_of_guesses):

        if guess_count < guess_limit:

            guess_kid = input("Enter another word : ")

            guess_count += 1

    else:

        out_of_guesses = True

    if out_of_guesses:

        print("Out of luck")

    else:

        print("you win")

// 三个错误输入后程序没有终止


达令说
浏览 125回答 2
2回答

MMMHUHU

缩进是一种触动。尝试这个:exact_word = "Guessing"guess_kid = ""guess_count = 0guess_limit = 3out_of_guesses = Falsewhile guess_kid != exact_word.casefold() and not(out_of_guesses):&nbsp; &nbsp; if guess_count < guess_limit:&nbsp; &nbsp; &nbsp; &nbsp; guess_kid = input("Enter another word : ")&nbsp; &nbsp; &nbsp; &nbsp; guess_count += 1&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; out_of_guesses = True&nbsp; &nbsp; &nbsp; &nbsp; if out_of_guesses:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Out of luck")&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("you win")使用循环/子句后跟踪缩进很重要。由于我们没有在while:循环之前缩进,我将前 5 行移动到完全左对齐。因为while:循环开始一个循环,所以下一个子句if和相应的else子句缩进一个。这些子句下的子集文本再次缩进。希望这可以帮助!

翻过高山走不出你

exact_word = "Guessing"guess_kid = ""guess_count = 0guess_limit = 3out_of_guesses = Falsewhile guess_kid != exact_word.casefold() and not(out_of_guesses):&nbsp; &nbsp; if guess_count < guess_limit:&nbsp; &nbsp; &nbsp; &nbsp; guess_kid = input("Enter another word : ")&nbsp; &nbsp; &nbsp; &nbsp; guess_count += 1&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; out_of_guesses = Trueif out_of_guesses:&nbsp; &nbsp; &nbsp; &nbsp; print("Out of luck")else:&nbsp; &nbsp; &nbsp; &nbsp; print("you win")这对你有用。一旦在 中进行了一些更改,您需要检查条件out_of_guesses。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python