Python printing after a while loop, involving sleep statements

44 views Asked by At

I have this function I wrote to do a simple type of half-way search for an integer, as a practice problem. When I run it, I would expect these print statements to be firing after each while loop, however it seems to stall until the correct answer is found, and then print each statement in rapid succession. Does this have to do with the sleep statement or am I missing something here?

def start_search(self):
        check_count = 1
        # loop a search function of some kind
        while not self.upper_bound == self.target_number:
            time.sleep(0.01)
            # get middle
            middle = self.get_half(self.lower_bound, self.upper_bound)
            print(f"checking between {self.lower_bound} and {self.upper_bound}")
            # check if target higher or lower than middle
            if middle > self.target_number:
                print(f"Midpoint of {middle} is higher,")
                self.upper_bound = middle
            elif middle < self.target_number:
                print(f"Midpoint of {middle} is lower,")
                self.lower_bound = middle
            else:
                print(f"Found value at midpoint {middle} after {check_count} check(s)")
                self.upper_bound = middle
            check_count += 1
1

There are 1 answers

0
Otis On

It didn't have to do with sleep at all. Debugger made it clear thanks to @MatsLindh that it was a different function stalling the program. Program was bare bones, so I guessed wrong that it was this function stalling everything.

Use your debugger folks.