如何使用多个条件进行while循环
我在Python里有一个while循环
condition1=False
condition1=False
val = -1
while condition1==False and condition2==False and val==-1:
val,something1,something2 = getstuff()
if something1==10:
condition1 = True
if something2==20:
condition2 = True
'
'
我想在所有这些条件都满足的时候跳出循环,但上面的代码不管用
我最开始写的是
while True:
if condition1==True and condition2==True and val!=-1:
break
这个方法还不错,这样做算是最好的方法吗?
谢谢
6 个回答
1
你有没有注意到你发的代码里,condition2
从来没有被设置为 False
?这样一来,你的循环里面的内容就永远不会被执行。
另外,在Python中,通常用 not condition
来表示条件不成立,而不是 condition == False
;同样的,直接用 condition
来表示条件成立,而不是 condition == True
。
3
while not condition1 or not condition2 or val == -1:
但是你在一个无限循环里面用if语句是完全没问题的。
23
把 and
改成 or
。