I need to call a function until it returns 0 in python

Question:

def solveMaze(win, board):  
    mazesol.removeDeadEnds(win, board)

I need to call mazesol.removeDeadends(win,board) until it returns 0. This is what the function does:

This function takes the window as its first argument and the board as its second argument. It sweeps the complete board (skipping the first and last rows and the first and last columns), and converts every position that is a path with exactly one path as a neighbor into a dead end. It returns the number of dead ends that were converted.

Asked By: Jostlyn

||

Answers:

I think this is what you want:

while mazesol.removeDeadEnds(win, board) != 0:
    pass
Answered By: David Z

Is there something wrong with:

while mazesol.removeDeadends(win,board): pass

or

while mazesol.removeDeadends(win,board): print ".",

or

 a = 1
 while a:
   a = mazesol.removeDeadends(win,board)
   print "Removed", a
Answered By: Carl F.

fast and dirty

result = mazesol.removeDeadends(win,board)
while not result:
  result = mazesol.removeDeadends(win,board)
Answered By: Vasiliy Stavenko

You could do this with an infinite while loop that breaks if 0 is returned:

While True:
    result = mazesol.removeDeadends(win,board)
    if result == 0:
        break
Answered By: Acorn
for _ in iter(partial(mazesol.removeDeadends, win, board), 0):
   pass

iter has a second form that allows you to pass a callable and a setinel value. What’s cool is that if you need to, you can loop over the response since it’s now an iterable.

Answered By: orpheus
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.