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.
Answers:
I think this is what you want:
while mazesol.removeDeadEnds(win, board) != 0:
pass
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
fast and dirty
result = mazesol.removeDeadends(win,board)
while not result:
result = mazesol.removeDeadends(win,board)
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
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.
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.
I think this is what you want:
while mazesol.removeDeadEnds(win, board) != 0:
pass
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
fast and dirty
result = mazesol.removeDeadends(win,board)
while not result:
result = mazesol.removeDeadends(win,board)
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
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.