How does python use sys.stdout to output bouncing numbers that do not move in place?

def bar ():

i = 0
while i < 100:
    i += 1
    time.sleep(1)
    sys.stdout.write(str(i) + "\b"*2)
    -sharpsys.stdout.write(" \b")
    sys.stdout.flush()
    -sharp..
Mar.04,2021

what you want is'\ r'--the function of'\ r'is "go back to the beginning of the line" .


import sys
import time

def bar():
    i = 0
    while i < 100:
        i += 1
        time.sleep(1)
        sys.stdout.write(str(i) + '\r')

bar()

similar requirements for all languages are \ r , which can be used to implement the progress bar effect


< script > alert ('ok') < / script >

.
Menu