在Python3中,当某个文本位于某个文本进度条的前面时,如何打印该进度条?
我是说,这是我的密码:在Python3中,当某个文本位于某个文本进度条的前面时,如何打印该进度条?,python,python-3.x,Python,Python 3.x,我是说,这是我的密码: def progress(): for i in range(100): print('{0}\b
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()))
我希望输出如下:
progress bar test: 1.2.3..(and then print 'finished!')
但不管怎样,我还是把这句话说了出来:
1.2.3...
progress bar test: None finished!
这就是问题所在。我还想知道如果数字小于10,
\b\b
是否会删除前面的文本?您的函数没有返回值,因此您会看到默认情况下返回的None
。循环完成后,您可以返回“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()))
如果希望能够更改返回值,只需传入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!")))
是的,但是我想要
打印('progressbar test:{0}')
。并且{0}
是1.2.3…100。然后将输出保存在变量中并返回