2

I'm trying to do the text refresh to be able to display some information in the same line, basically rewriting the old text.

code sample:

from time import sleep

def countdown(n):
    if n > 0 :
        print(n)
        sleep(0.3)
        countdown(n-1)
    else :
        print("Blastoff!")
countdown(3)

If I use the "\r" in the print statement, it does \n two times.

I tried the carriage return "\r" and the cls clear() function, everything on stack, but nothing works for Mac.

code sample:

from time import sleep

def countdown(n):
    if n > 0 :
        print(n)
        sleep(0.3)
        countdown(n-1)
    else :
        print("Blastoff!")
countdown(6)

If I use the "\r" in the print statement, it does \n two times. this is the current output: 3 2 1 Blastoff!

in the same window.

i'd like to have the output as follows: 3 # in the first line

2 # in the same spot that 3 was.. and so on. . . Blastoff! # at the end will be the only thing on the screen.

Community
  • 1
  • 1

1 Answers1

3

The print statement appends a new line at the end of your output by default. To prevent this you want to add end="" as parameter. Now your output will always be on the same line. In addition to this you want to use "\r" at the beginning of you output.

If you would add "\r" at the end of the output some consoles might overwrite your output while doing the carriage return and you would end up with a blank line.

I'd also avoid using recursion here, iteration is usually the faster way and unless it over complicates your code I - personally - would prefer it.

def countdown(n):
    while 0 < n:
        # add some spaces after the number to make sure we overwrite the entire last output
        print("\r%d   " % n, end="")
        n -= 1
        sleep(0.3)
    print("\rBlastoff!")

Edit

Taken from here: Overwrite printed line in python 3.x on mac

Replace print(...) with sys.stdout.write(...) aswell as

def countdown(n):
    while 0 < n:
        sys.stdout.write("\r%d   " % n)
        sys.stdout.flush()
        n -= 1
        sleep(0.3)
    sys.stdout.write("\rBlastoff!")
    sys.stdout.flush()

You will also need to import sys

Kami Kaze
  • 611
  • 3
  • 14
  • it does not work. it outputs 2 1 0 Blastoff! with two spaces between the lines. i want that all in one line and rewritten. – QuadFacedDog Jan 01 '19 at 13:06
  • Are you using python 2? – Kami Kaze Jan 01 '19 at 13:08
  • i am using 3.7.1 – QuadFacedDog Jan 01 '19 at 13:13
  • Hmm... that's strange, the snippet I gave in my answer should work just fine. You could try adding `import os` to your imports and `os.system('cls')` after `sleep(0.3)`. This would clear the console after you printed something. – Kami Kaze Jan 01 '19 at 13:24
  • the same output, maybe it has to do something that i am running python on mac? i'm getting kinda desperate here. nothing works. – QuadFacedDog Jan 01 '19 at 17:01
  • I do not use mac myself but it does seem to make a difference (according to google). I updated my answer, go try it again ;) – Kami Kaze Jan 01 '19 at 17:35
  • Kanjiu i tried it that way, and with multiple variations from the question you mentioned. it now prints in one line but liek this : 321Blastoff! , nothing gets refreshed but i guess we made a progress :D – QuadFacedDog Jan 01 '19 at 19:26
  • Well - some progress, sigh - So after knowing you are using mac os I did some more reading and it seems the console command to clear the console on mac os is`clear` not `cls` as on windows. You could try verifying this by simply opening a console and typing `clear` to see if it clears the console. If it does you can use the approach I pointed out in my second comment but use `os.system('clear')`. If not we'll have to keep digging... – Kami Kaze Jan 02 '19 at 06:35
  • 1
    i found out that clearing the screen from IDLE in python is impossible without external modules. There is no workaround that. Thanks for your time @Kanjiu , glad there was someone willing to help :) have a nice day – QuadFacedDog Jan 02 '19 at 11:16