Its also not entirely clear what you mean by
Is the issue that you want to end the loop early, after fewer iterations? That each individual iteration takes too long? Or that you want to print Working fine
while also running some code that’s not necessarily in a loop? There’s a wide variety of potential solutions, depending on what your actual issue is and what you want to achieve here.
To end the loop early, use the break
statement. and to make the loop run until an arbirary time (e.g. your code is done), use a while
loop. As a simple example:
print("BEGIN")
while True:
# Do something
status = do_something()
# Etc
print("Working...")
if status == "done":
break
print("END")
If you want your individual sleep iterations to be shorter, so the program terminates nearly immediately if you interrupt it (e.g. Ctrl-C
), you can use a shorter interval for your sleeps, and more of them, e.g.
SLEEP_INTERVAL_S = 0.1
SLEEP_LENGTH_S = 5
print("BEGIN")
for counter in range(SLEEP_INTERVAL_S // SLEEP_LENGTH_S):
percent_elapsed = SLEEP_INTERVAL_S * counter / SLEEP_LENGTH_S
print(f"{round(percent_elapsed)}% complete)
sleep(SLEEP_INTERVAL_S)
print("END")
And if you want your program to print output while its doing something else, the simplest way, if your task involves a loop of some sort, is just embed a progress-reporting print call in the loop body, so you get an update with every iteration of the loop (e.g. as in the above example). If your code doesn’t use a loop, you could scatter descriptive prints (or logging calls, etc) in various important points in your code.
You could also run your code in another coroutine, thread, Python process, OS-level processing by using async
, threading
, multiprocessing
and subprocess
respectively, and have your main thread handle the prints (or vice versa), but those are all more advanced techniques and probably overkill here, whereas the above approaches generally provide you with much more useful output than just that simply your code is still running.
This code contains an apparent mistake. You’re sleeping for a duration determined by the loop counter, rather than a constant time (e.g. 1 second), so the first iteration sleeps 0 seconds, the second 1, the third 2, and so on, for a total of 10 seconds rather than five, and unevenly distributed between iterations. Presumably, you mean something like
for __ in range(5):
print("Working...")
sleep(1)
This code also contains a bit of a mistake: you’re iterating through the loop counter (perhaps a little confusingly called loop
) but then also incrementing it manually every iteration, which doesn’t actually do anything. Also, working()
is presumably your business code while the print()
can go in the loop block, unless it needs access to working()
's state (which perhaps could be better named either do_something()
, or print_working()
, as function names should be verbs that describe what action they do), reversing what appears to be the current structure.
And as an aside, you generally want to keep the sleep interval fairly short, ideally around 0.1 s or less and certainly not less than 1 s, as particularly on some platforms, like on Windows, interrupts don’t get processed during sleep so you won’t break out of the loop or do anything else until sleep()
finishes, and it ensure your program is more responsive.