我正在寻求正确使用以下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)
您可以通过将所有内容移动到一个循环中来很好地压缩它:
while True:
choice = input("Choose key: ")
if choice in ("0", "1"):
choice = int(choice)
break
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
75 次 |
| 最近记录: |