简体   繁体   English

如何编写while循环以使其在2个变量获得某些值时停止? (Python)

[英]How can I write while loop in order to make it stop when 2 variables got certain values? (Python)

I have the following code (in Python):我有以下代码(在 Python 中):

while i != 0 and j != 0:
  possibAct, posX, posY = possibleActions(i, j)
  i, j = constructValueFunction_GetOptimalAction(possibAct, posX, posY)
  possibAct, i, j = possibleActions(i, j)

Seems like it stops when only one (i or j) gets to 0, but what I want is to make it stop only when both are 0. What am I missing?似乎只有一个(i 或 j)为 0 时它会停止,但我想要的是让它只有在两者都为 0 时才停止。我错过了什么?

You can give or like this:你可以给或像这样:

while i!=0 or j!=0:
   (some code)

when i is 0,j may not be 0 so it waits for j to become 0当 i 为 0 时,j 可能不为 0,所以它等待 j 变为 0

With your current code, loop will break when any of i,j gets 0 as i.=0 and j,=0 becomes False: You need something like the following, in order to stop the loop only if both i and j become 0:使用您当前的代码,当 i,j 中的任何一个为 i.=0 并且 j,=0 变为 False 时,循环将中断:您需要类似以下内容,以便仅当 i 和 j 都变为 0 时才停止循环:

while not (i == 0 and j == 0):
  possibAct, posX, posY = possibleActions(i, j)
  i, j = constructValueFunction_GetOptimalAction(possibAct, posX, posY)
  possibAct, i, j = possibleActions(i, j)

You can just replace and with or您可以将and替换为or

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我写一个字符串时,如何使 integer 在“while true”循环中停止? - How to make an integer in a "while true" loop stop when I write a string? 如何使此循环在某个变量处停止 - How can I make this loop stop at a certain variable occurs 当输入无效时,如何停止 for 循环迭代? (不允许使用 while 循环)- Python - How can i stop a for-loop iterating when the input is not valid? (not allowed to use a while loop) - Python 如何使用Python添加在'while'循环中生成的某些值 - How can I add certain values that were produced in a 'while' loop using Python 在python中设置变量时,如何使一个多进程循环停止另一个? - How can I make one multiprocessed loop stop another when a variable is set in python? Python:如何用 while 循环替换这个 for 循环? (没有负值) - Python: How can I substitute this for loop with a while loop? (No negative values) 我如何使python循环重复两个变量 - How can i make python loop repeating two variables 当达到某个索引值时,如何停止将数字添加到列表中的 while 循环? - How do I stop a while loop that adds numbers to a list when a certain index value is reached? 如何停止 While 循环? - How can I stop a While loop? 如何在仍然可以循环的同时阻止这些变量重置? - How can I stop these variables from resetting while still being able to loop them?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM