The main difficulty is making sure that Kara does not endlessly walk up through an exit to the next row and then back down again. He must remember whether he is looking for an exit to his right or to his left. He must ignore exits on the other side. The following extract of the main programs shows how two boolean variables can be used to search fo the next exit:

ignoriereLochLinks = 0
ignoriereLochRechts= 1

while not kara.onLeaf():
  while (ignoriereLochLinks or kara.treeLeft()) and (ignoriereLochRechts or kara.treeRight()):
    if not kara.treeFront():
      kara.move()
    else:
      ignoriereLochLinks = not ignoriereLochLinks
      ignoriereLochRechts= not ignoriereLochRechts
      kara.turnLeft()
      kara.turnLeft()
# ...

kara.removeLeaf()