为什么我不能中断也不能退出while循环

时间:2019-06-13 15:14:06

标签: python python-3.x while-loop

由于某种原因,当我键入“ exit”或“ EXIT”时,循环会继续迭代

我尝试使用变量和break语句中断

obj = 1
while obj != None:
    a = input("enter a number:\n")
    b = input("enter a number:\n")
    try:
        int(a)
        int(b)
        print("succes!")
    except ValueError:
        print("you didnt enterd numbers")
        continue
    print("what do yo want to do?\n")
    donxt = input("for exit the program type EXIT, to continue tap CONTINUE:\n")
    if donxt == "continue" or "CONTINUE":
        continue
    elif donxt == "EXIT" or "exit":
        obj = None

1 个答案:

答案 0 :(得分:3)

您有if donxt == "continue" or "CONTINUE"

这实际上是if (donxt == "continue") or ("CONTINUE")而不是if (donxt == "continue") or (donxt == "CONTINUE")

"CONTINUE"始终取值为true,因此(donxt == "continue") or ("CONTINUE")始终为true。

您要执行以下操作: if (donxt == "continue") or (donxt == "CONTINUE")