1

I want to show only one item at the screen I guess this should be done with print(..., flush=True) but it don't work as I expected.

for i in range(0,100):
    print(i, end='', flush=True)

Now I get a numbers like this 012345678.... and what I want to see at the screen is only one number without seeing previous prints so with first iteration it shows only 0 second shows only 1, I thought flush would do the trick but it didn't so where am I wrong?

2 Answers2

5

You can use end='\r' as an argument to print(). \r escape sequence moves the cursor to the starting of the line. flush is not the correct argument for doing this.

Your code can be:

for i in range(0,100):
    print(i, end='\r')
1

You can make use of os module to do the same

import os
import time

for i in range(100):
    print(i)
    time.sleep(1)
    os.system("clear") # For Linux
    #os.system("cls") # For Windows


I have used time.sleep() to make sure the effect is visible otherwise it executes very fast. You may make variations as per the need.

Tanishq Vyas
  • 1,422
  • 1
  • 12
  • 25