Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can we print a text only once that is inside a loop without making it print again and again until the loop ends?

First of all, I’m a newbie. I am trying to make a countdown. Now when I use a while loop to achieve that I also need to print count down but what happens is that the countdown takes place in separate lines printing the text over and over again. I want the loop to print the text only once but keep changing the value of countdown until countdown is over.

my code is:

      clearing = True
      countdown = 3
      while clearing:
        while countdown > 0:
          print(f"Clearing console in {countdown} second(s)")
          time.sleep(1)
          countdown -= 1
        os.system('clear')
        clearing = False

and this is the result I am currently achieving in the console rn:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Clearing console in 3 second(s)
Clearing console in 2 second(s)
Clearing console in 1 second(s)

see the print repeats evertime until countdown == 0
what I want is for the loop to print the text only once but keep changing the value of countdown inside the text

something like this:

Clearing console in '3 then 2 then 1' second(s)

>Solution :

Well, actually there is something you can do about it.

print(f"Clearing console in {count}  second(s)", end='\r', flush=True)

I suggest you to read what carriage return is and something about flush too.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading