-1

Each second, it prints a new line. Is there a way to have it print ontop of the previous line?

while True:
    sec += 1
    if sec / 60 == sec_int:
        sec = 0
        mins += 1
        if mins / 60 == min_int:
            mins = 0
            hours += 1
            if hours / 24 == hour_int:
                hours = 0
                days += 1
    print(f"{days}d : {hours}h : {mins}m : {sec}s")
    time.sleep(1)
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 1
    It's easier if you paste the code instead of an image. `end='\r' might work in the print: see https://stackoverflow.com/questions/3419984/print-to-the-same-line-and-not-a-new-line – doctorlove Nov 19 '22 at 18:44
  • 2
    Not just easier, we _outright disallow_ questions that only include images or links and not enough information to concretely answer the question or learn from answers with only what's in the body text itself; the [mre] definition requires a reproducer to be included _in the question itself_. See [Why should I not upload images of code/data/errors?](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors) -- it's unreasonable to ask someone to retype text from your screenshot to be able to see a problem themselves or test a proposed answer. – Charles Duffy Nov 19 '22 at 18:49

1 Answers1

0

Replace your print statement with:

print(f"\r{days}d : {hours}h : {mins}m : {sec}s", end="", flush=True)

"\r" is a "control character" which moves the cursor to the beginning of the line ("carriage Return"). flush=True is needed to make the display update right away--normally Python can buffer until a newline is written, which of course you'll never write (because there's no way to go back up to a previous line).

John Zwinck
  • 239,568
  • 38
  • 324
  • 436