while int(input()) != 1:
# <logic for else>
pass # only put this if there's no logic for the else.
print("Hi!")
尽管有其他方法,如:
choose = int(input())
while choose != 1:
<logic for else>
choose = int(input())
或者你可以创建一个函数:
def poll_input(string, expect, map_fn=str):
"""
Expect := list/tuple of comparable objects
map_fn := Function to map to input to make checks equal
"""
if isinstance(expect, str):
expect = (expect,)
initial = map_fn(input(string))
while initial not in expect:
initial = map_fn(input(string))
return initial
这有点奇怪,它适用于期望值为布尔值(仅两个期望值)的情况,这些布尔值不是0就是1,而不是其他任意字符串,aa和您不想存储输入的情况。你知道吗
尽管有其他方法,如:
或者你可以创建一个函数:
因此使用它:
对于更模糊的情况
相关问题 更多 >
编程相关推荐