0

I have created a simple python program that will count up to 10

What I am trying to achieve is for the program to delete the previous number and print the new number

This is the code that I have created:

import sys
import time
for i in range(10):
    text = "\r" + str(i)
    sys.stdout.write(text)
    sys.stdout.flush()
    time.sleep(1)

Which outputs:

0123456789

Changing the code to have the "\r" after the str(i) didn't work either

import sys
import time
for i in range(10):
    text = str(i) + "\r"
    sys.stdout.write(text)
    sys.stdout.flush()
    time.sleep(1)

Which also resulted in:

0123456789

I was looking for it to count up, and display at the end only the 9 however it doesn't overwrite the previous numbers

Edit:

I am using Windows, Python3

Edit 2:

How to overwrite the previous print to stdout in python? Does not give me a working answer, it still doesn't do what I want

Therefore due to my excellent reasoning it is not a duplicate :P

Rlz
  • 1,649
  • 2
  • 13
  • 36

6 Answers6

3

As an alternative, you can clear the whole window, by using os.system("cls")

1

There are some control symbols accepted by virtual terminals. One of them is '\b' that moves a carret for one place back. This one is accepted on Windows too and I will use it in my example below. Unix terminals accept a lot more of controls including color changes and more.



from time import sleep
import sys, os

def clear ():
    os.system("cls" if sys.platform.startswith("win") else "clear")

clear()

s = "1"
sys.stdout.write(s)
for x in range(2, 21):
    sleep(1)
    # Return carret to beginning of line:
    l = len(s)
    s = l*"\b"
    sys.stdout.write(s)
    # Clear line (just in case):
    s = l*" "
    sys.stdout.write(s)
    # Return to the beginning again:
    s = l*"\b"
    sys.stdout.write(s)
    # Write over new text:
    s = str(x)
    sys.stdout.write(s)

Dalen
  • 4,128
  • 1
  • 17
  • 35
  • Dalen unfortuantly this does not work for me. This is the output (Note its all on separate lines): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 – Rlz Aug 30 '17 at 11:51
  • @RulerOfTheWorld : Did you run it in terminal or in IDLE or some other GUI console? It is possible that the GUI window doesn't like '\b' – Dalen Aug 30 '17 at 11:55
  • @Dalen I am running this in IDLE – Rlz Aug 30 '17 at 11:57
  • @RulerOfTheWorld : then run it in console. – Dalen Aug 30 '17 at 12:01
  • @RulerOfTheWorld : Here you go! This one works! The trouble was that Windows cannot go one line up with '\b', just inside the current line. So you cannot use '\n'. Save it and start it as a script. Using it in an interpreter interactively will mess things up. – Dalen Aug 30 '17 at 13:43
0

Use the end parameter to specify a "\r" as the line ending.

import time
for i in range(10):
    print(i, end="\r")
    time.sleep(1)
paullb
  • 4,293
  • 6
  • 37
  • 65
-1

I would do it like this:

for x in range(10):
    print("{}".format(x), end="\r")
Eduardo
  • 631
  • 1
  • 12
  • 25
-1

Why not use the standard print of python? Althougth given in many debugger screens this tends to not work. In terminal (ubuntu) it does

import time

for i in range (10):
    print(i, end='\r')
    time.sleep(1)

Probably for stdout something exists too but in this case I usually use the print of python3

davy gillebert
  • 307
  • 1
  • 11
  • Just took a second look and here in the console it works perfectly. In the run of pycharm IDE it doesn't, so I'm thinking of mainly something odd witht he IDE's – davy gillebert Aug 30 '17 at 11:51
-1

I believe your code should work perfectly fine. The problem should be that you are using a Windows terminal. Try using Linux. Most code does not work in Windows. You can refer this link to know why you have to stop using python on Windows.

This is also an alternate code you can try.

    import time
    for i in range(10):
        text = str(i)
        print(text,end = "\r")
        time.sleep(1)