简体   繁体   中英

Percentage to progress bar

I'm trying to convert a percentage to a progress bar and I've written the code but I believe that it can be improved and made a lot smaller.

If the total chars are 10, then it will be a progress bar that's 10 characters long, ideally, I also want to do this for 20, 30, and 40 character long progress bars.

This is the code that I have for a 10 character long progress bar.

if int(total_chars) == 10:

    if int(percent) in range(0,10):
        final = "'=         '"
    if int(percent) in range(10,20):
        final = "'==        '"
    if int(percent) in range(20,30):
        final = "'===       '"
    if int(percent) in range(30,40):
        final = "'====      '"
    if int(percent) in range(40,50):
        final = "'=====     '"
    if int(percent) in range(50,60):
        final = "'======    '"
    if int(percent) in range(60,70):
        final = "'=======   '"
    if int(percent) in range(70,80):
        final = "'========  '"
    if int(percent) in range(80,90):
        final = "'========= '"
    if int(percent) in range(90,101):
        final = "'=========='"

    print(final)
if int(percent) == 100:
    print(10 * '=')
else:
    p = percent[0]
    p = int(p)
    print('=' + '=' * p)
from math import ceil
def bar(porcent):
    nearest_ten = int(10 * ceil(float(porcent)/10))
    x = int(nearest_ten/10)
    return (("="*x)+(" "*(10-x)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM