Python 如果答案无效,则退出while循环

Python 如果答案无效,则退出while循环,python,Python,如果答案无效,我希望用户退出while循环。当我选择一个无效响应时,它会将exitchoice的值更改为“EXIT”,但实际上它不会退出循环。我认为它应该退出循环,因为while循环处于活动状态的条件是exitchoice!=“退出”,因此当设置为“退出”时,循环应中断。也许我错了,或者python不是这样工作的 exitchoice = "Nothing" while exitchoice.upper() != "EXIT": confirm =

如果答案无效,我希望用户退出while循环。当我选择一个无效响应时,它会将exitchoice的值更改为“EXIT”,但实际上它不会退出循环。我认为它应该退出循环,因为while循环处于活动状态的条件是exitchoice!=“退出”,因此当设置为“退出”时,循环应中断。也许我错了,或者python不是这样工作的

exitchoice = "Nothing"
while exitchoice.upper() != "EXIT":
    confirm = input("Is this what you want? ")
    if confirm.upper() == "NO":
        print("Returning to selection in ")
        print("3")
        print("2")
        print("1")
    elif confirm.upper() == "YES":
        print("Enjoy your Journey...")
    else:
        print("Invalid, please try again. ")
        exitchoice = "EXIT"
    print("Continue Code blah blah blah")

当输入是有效响应之一时,而不是当输入无效时,应将
exitchoice
设置为
'EXIT'

exitchoice = "Nothing"
while exitchoice.upper() != "EXIT":
    confirm = input("Is this what you want? ")
    if confirm.upper() == "NO":
        print("Returning to selection in ")
        print("3")
        print("2")
        print("1")
        exitchoice = "EXIT"
    elif confirm.upper() == "YES":
        print("Enjoy your Journey...")
        exitchoice = "EXIT"
    else:
        print("Invalid, please try again. ")
    print("Continue Code blah blah blah")
或者,在满足条件时,使用
while True:
循环和
中断更为惯用:

while True:
    confirm = input("Is this what you want? ")
    if confirm.upper() == "NO":
        print("Returning to selection in ")
        print("3")
        print("2")
        print("1")
        break
    elif confirm.upper() == "YES":
        print("Enjoy your Journey...")
        break
    else:
        print("Invalid, please try again. ")
    print("Continue Code blah blah blah")
或者,如果存在多个有效响应,则不要向每个有效条件添加
break
,而是向无效条件添加一个
continue
,默认情况下让循环
中断

while True:
    confirm = input("Is this what you want? ")
    if confirm.upper() == "NO":
        print("Returning to selection in ")
        print("3")
        print("2")
        print("1")
    elif confirm.upper() == "YES":
        print("Enjoy your Journey...")
    else:
        print("Invalid, please try again. ")
        continue
    print("Continue Code blah blah blah")
    break

尝试使用
String
Class
方法

while not exitchoice.upper().__eq__("EXIT"):
    # Your code inside loop

使用
Class
方法更改while条件,并按预期运行。

我的知识有限,但您在
while!=退出
循环,该循环将不会退出,因为while循环意味着它将执行,直到条件变为false。我认为您应该使用一个if循环,然后在其中放入另一个if语句,如下所示:

    if exitchoice.upper() != "EXIT":
        confirm = input("Is this what you want? ")
        if confirm.upper() == "NO":
            print("Returning to selection in ")
            print("3")
            print("2")
            print("1")
        else confirm.upper() == "YES":
            print("Enjoy your Journey...")
   else:
       print("Invalid, please try again. ")
       exitchoice = "EXIT"
       print("Continue Code blah blah blah")```

您的代码正在按预期工作。你能再查一下吗?还是重新构思你的问题?