Reputation: 89
I want to make code so that a number goes from 1 to 2 to 3 and so on.
I am running this on Python 3. I have tried:
import time
from sys import stdout
for i in range(1,20):
stdout.write("\r%d" % i)
stdout.flush()
time.sleep(1)
stdout.write("\n") # move the cursor to the next line
But with that code, I get 12345678910111213141516171819. Can someone help? EDIT: I ran this code from the console, not from IDLE and it works. Can anyone explain?
Upvotes: 0
Views: 165
Reputation: 542
For my setup it works (Arch Linux, Python3). Do you start from IDE? Or from console?
Maybe another way works for you:
import time
from sys import stdout
for i in range(1,20):
print(i)
time.sleep(0.1)
stdout.write("\033[F") #back to previous line
stdout.write("\033[K") #clear line
stdout.write("\n") # move the cursor to the next line
Idea found here. This example works in my console but not when I start from PyCharm directly.
Upvotes: 1