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:
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.
- https://www.ascii-code.com/glossary/carriage-return
- How can I flush the output of the print function?