简体   繁体   中英

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):

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?

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM