While Kara is not on a leaf, he tests whether he can step ahead or turn left or turn right, and takes the appropriate action.
while not kara.onLeaf(): if not kara.treeLeft(): kara.turnLeft() kara.move() elif not kara.treeFront(): kara.move() elif not kara.treeRight(): kara.turnRight() kara.move() kara.removeLeaf()