-1

I have tried all the scripts mentioned in other questions, but not working for me.

for x in range (0,10):
    print(x, end='\r', flush=True)
    print(x, sep='\r', end='', flush=True)
    print('\r', x)

Please help on this.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
Learnings
  • 2,780
  • 9
  • 35
  • 55

3 Answers3

2

You probably want to overwrite the text, right?

You need to write backspaces to delete the last line and then print the next one. Check out this post for an example: How to create a spinning command line cursor?

rdas
  • 20,604
  • 6
  • 33
  • 46
1

Perhaps, you want a time.sleep(1) to have a better visualization, using stdout.write:

import sys
import time
for i in range(5):
    sys.stdout.write('\r' + str(i))
    time.sleep(1)

OUTPUT:

enter image description here

DirtyBit
  • 16,613
  • 4
  • 34
  • 55
1
>>> for x in range(10):
...  print('\r', x, end='')
... 
 9>>> 

As you can see, all the other numbers got overwritten because to the carriage-return symbol \r.

ForceBru
  • 43,482
  • 10
  • 63
  • 98