Needing Help On A basic project

Hello, Dear Python developers,

My team and I are currently working on a project, but we are having a small problem with our code.
As I am going to compare it with some basic functions to understand it better, we would be happy to get some tips or even better if you could write the solution. Thanks in advance.

The code is:
print(“Keep going”)

We want it to continuously start printing “Working fine” after the output is BEGIN, and end it in exact time when the output is END.

Not a complete solution, but this should get you going…

from time import sleep
print("Keep going")
for s in range(5):
    print("Working fine")

Thank you Rob, this is good solution for the beginning.

The reason why we couldn’t use for or while loops is because we have to wait until the loop is over every time to exit. Will this fix it? Is there a chance to print “Working Fine” while the program is ‘keeping going’, like we want to have the output “Working Fine” to know the program is still working, and if the output stops showing up we know it ended?

I’m not entirely sure what you’re aiming at, but if it’s simply to keep on printing the statement “Working fine” while some arbitrary code is running, I would introduce a custom function call like this:


def working():
    print("Working fine")

# Any arbitrary routine, that you want to monitor#
for loop in range(100):
    loop +=1


That’s simply a loop that terminates after it hits 100, but it could be any code you choose.

I tend to do this kind of thing as a custom debugger; just call a custom function and have it output whatever I want it to.

Thank you again Rob,
Seems like this was the one we were trying to do. We will test it with the program, to give more details we will place it in this below:

from djitellopy import tello
import cv2
import sleep

drone.streamon() #Getting frame

while True:



    cv2.imshow("Pencere", resim)


as you can see we want to get the frames while it is flying

No worries.

I can see the context now; cool.

Good luck with it.

{edit to add}

btw, if I’ve totally missed the target and what I’ve posted is of little or no use, just say as much; I’ll not be offended. That way someone with way more experience than I, may very well chip in with a clever solution and I’ll also learn something new, which is my primary goal in being here.

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:

while True:
    # Do something
    status = do_something()
    # Etc
    if status == "done":

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.


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)

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):

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.

1 Like