How to give multiple python prints in the same place as the terminal?

Asked

Viewed 474 times

1

Having the following code as the basis:

for x in range(100):
    print("{}%".format (x))

How could it be done so that in place of each value appears in a different row and yes replaced in the same row.

Example instead of having in the terminal:
1
2
3
4
5
First have the 1 after it "fade" and show the 2 and so on until you get to the 5?

I know you can do it using C.

1 answer

0


I did some research and I managed to do it that way:

import sys
import time
for x in range(101):
    print("{}%".format (x))
    sys.stdout.write("\033[F")
    time.sleep(0.1)

What causes the text to be replaced is this call: sys.stdout.write("\033[F"), from what I understand she clears the line.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.