首页 > 解决方案 > Python 下载脚本

问题描述

当我运行它时,Progress % 是倒退的,有谁知道如何在开始时使其为 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

标签: pythonpython-3.x

解决方案


只需使用(1-x/y)而不是x/yin 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%才能正确显示。


推荐阅读