while循环正确嵌套

Eva*_*van 0 python python-3.x

我正在寻求正确使用以下3个while循环的帮助:

while choice is None:  ...
while not isinstance (choice, int):  ...
while int(choice) not in range(0,1):  ...
Run Code Online (Sandbox Code Playgroud)

也许这样的东西:

while choice is None and not isinstance (choice, int) and int(choice) not in range(0,1):
    print("Invalid option!")
    choice = input("Choose key: ")
Run Code Online (Sandbox Code Playgroud)

我怎么能正确地嵌套这个?

choice = None
choice = input("Choose key: ")

while choice is None:
    choice = input("Choose key: ")

while not isinstance (choice, int):
    print("choice is an integer and I equal 0 or 1")
    print("Also if I am None or not an int, I will loop until I meet I am")

while int(choice) not in range(0,1):
    choice = input("Choose key: ")
    choice = int(choice)
Run Code Online (Sandbox Code Playgroud)

chr*_*ris 5

您可以通过将所有内容移动到一个循环中来很好地压缩它:

while True:
    choice = input("Choose key: ")
    if choice in ("0", "1"):
        choice = int(choice)
        break
Run Code Online (Sandbox Code Playgroud)