简体   繁体   English

在python 3中,当某些文本位于文本进度条前面时,如何打印文本进度条?

[英]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?而且我也想知道如果数字小于10, \\b\\b删除前面的文字吗?

You function is not returning a value so you see None which is returned by default.您的函数没有返回值,因此您会看到默认情况下返回的None You can return "finished" when your loop completes:循环完成"finished"您可以返回"finished"

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:如果您希望能够更改返回值,只需传入一个 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!")))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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