如何执行具有多个条件的 while 循环

发布于 2024-08-19 04:31:29 字数 510 浏览 6 评论 0原文

我在 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

代码可以正常工作,这是最好的方法吗?

谢谢

I have a while loop in python

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

'
'

I want to break out of the loop when all these conditions are true, the code above does not work

I originally had

while True:
      if condition1==True and condition2==True and val!=-1:
         break

which works ok, is this the best way to do this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

流云如水 2024-08-26 04:31:29

and 更改为 or

Change the ands to ors.

无尽的现实 2024-08-26 04:31:29
while not condition1 or not condition2 or val == -1:

但是你原来在一段时间内使用 if 的做法没有任何问题 True 。

while not condition1 or not condition2 or val == -1:

But there was nothing wrong with your original of using an if inside of a while True.

飘过的浮云 2024-08-26 04:31:29

您是否注意到在您发布的代码中,condition2 从未设置为False?这样,你的循环体就永远不会被执行。

另请注意,在 Python 中,not condition 优先于 condition == False;同样,condition 优先于 condition == True

Have you noticed that in the code you posted, condition2 is never set to False? This way, your loop body is never executed.

Also, note that in Python, not condition is preferred to condition == False; likewise, condition is preferred to condition == True.

娇纵 2024-08-26 04:31:29
condition1 = False
condition2 = False
val = -1
#here is the function getstuff is not defined, i hope you define it before
#calling it into while loop code

while condition1 and condition2 is False and val == -1:
#as you can see above , we can write that in a simplified syntax.
    val,something1,something2 = getstuff()

    if something1 == 10:
        condition1 = True

    elif something2 == 20:
# here you don't have to use "if" over and over, if have to then write "elif" instead    
    condition2 = True
# ihope it can be helpfull
condition1 = False
condition2 = False
val = -1
#here is the function getstuff is not defined, i hope you define it before
#calling it into while loop code

while condition1 and condition2 is False and val == -1:
#as you can see above , we can write that in a simplified syntax.
    val,something1,something2 = getstuff()

    if something1 == 10:
        condition1 = True

    elif something2 == 20:
# here you don't have to use "if" over and over, if have to then write "elif" instead    
    condition2 = True
# ihope it can be helpfull
空袭的梦i 2024-08-26 04:31:29

我不确定它会读起来更好,但你可以执行以下操作:

while any((not condition1, not condition2, val == -1)):
    val,something1,something2 = getstuff()

    if something1==10:
        condition1 = True

    if something2==20:
        condition2 = True

I am not sure it would read better but you could do the following:

while any((not condition1, not condition2, val == -1)):
    val,something1,something2 = getstuff()

    if something1==10:
        condition1 = True

    if something2==20:
        condition2 = True
成熟稳重的好男人 2024-08-26 04:31:29

使用像您最初所做的那样的无限循环。它是最干净的,您可以根据需要合并许多条件

while 1:
  if condition1 and condition2:
      break
  ...
  ...
  if condition3: break
  ...
  ...

use an infinity loop like what you have originally done. Its cleanest and you can incorporate many conditions as you wish

while 1:
  if condition1 and condition2:
      break
  ...
  ...
  if condition3: break
  ...
  ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文