运行此命令时,进度%向后,是否有人知道如何使其开头为0%,完成时为100%?
import time
x = 25
y = x
t = 0
downloading = True
while downloading:
time.sleep(1)
t += 1
x -= 1
f = ((x/y) * 100)
print('Time:', str(t) + ',', 'Progress: ', '{0:.2}'.format(str(f)) + '%,', 'Remaining: ' + str(x), 'MB', end="\r")
if(x == 0):
print('\nComplete!')
break
答案 0 :(得分:2)
只需使用(1-x/y)
而不是x/y
中的f
。
import time
x = 25
y = x
t = 0
downloading = True
while downloading:
time.sleep(0.01)
t += 1
x -= 1
f = ((1-x/y) * 100)
print('Time:', str(t) + ',', 'Progress: ', '{0:.3}'.format(str(f)) + '%,', 'Remaining: ' + str(x), 'MB', end="\r")
if(x == 0):
print('\nComplete!')
break
还请注意,您应该使用'{0:.3}'.format(str(f))
,以便可以正确显示100%
。