简体   繁体   中英

How to print a text progress bar when some text was in front of it in python 3?

Well, I mean this, here's my code:

def progress():                                                                                                                         
    for i in range(100):                                                        
        print('{0}\b\b'.format(i), end='', flush=True)                          
        time.sleep(0.1)                                                         

print('progress bar test: {0} finished!'.format(progress()))    

I want the output like this:

progress bar test: 1.2.3..(and then print 'finished!')

but however I got this out put:

1.2.3...
progress bar test: None finished!

This is the question. And I also want to know if the number was smaller than 10, will the \\b\\b delete the in front text?

You function is not returning a value so you see None which is returned by default. You can return "finished" when your loop completes:

import time
def progress():
    for i in range(3):
        print('{0}.'.format(i), end='', flush=True)
        time.sleep(0.1)
    return "finished!"

print('progress bar test: {0}'.format(progress()))

If you want to be able to change the return value just pass in an arg:

def progress(end):
    for i in range(3):
        print('{0}.'.format(i), end='', flush=True)
        time.sleep(0.1)
    return end

print('progress bar test: {0} '.format(progress("finished!")))

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