Python to print out status bar and percentage

后端 未结 20 1088
野的像风
野的像风 2020-11-28 01:01

To implement a status bar like below:

[==========                ]  45%
[================          ]  60%
[==========================] 100%

20条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 01:03

    Using @Mark-Rushakoff answer, I worked out a simpler approach, no need to call the sys library. It works with Python 3. Tested in Windows:

    from time import sleep
    for i in range(21):
        # the exact output you're looking for:
        print ("\r[%-20s] %d%%" % ('='*i, 5*i), end='')
        sleep(0.25)
    

提交回复
热议问题