Answer the question
In order to leave comments, you need to log in
Proper display of the progressbar in Python - what's wrong?
Good evening!
Tell me, please, I can not understand why the progressbar is displayed incorrectly?
I would like to get it so that the speed of increasing the percentage value and filling the progress bar corresponds to the number of lines in the li list.
I would love your explanations!
Thank you, regards,
Ivan.
import requests
import time
import random
import sys
li = ["25min", "tester", "testsetset"]
status404 = []
status200 = []
lenLi = len(li) + 1
def wait():
time.sleep(random.randint(2,3))
for x in li:
link = "https://vk.com" + x
q = requests.get(link)
stat = q.status_code
if 200 != stat:
status404.append(x.rstrip())
wait()
else:
status200.append(x.rstrip())
wait()
for i in range(lenLi):
sys.stdout.write('\r')
sys.stdout.write("[%-30s] %d%%" % ('='*i, i))
sys.stdout.flush()
print("404 status")
for i in status404:
print(i)
print("\n- - - - -\n200 status:")
for i in status200:
print(i)
Answer the question
In order to leave comments, you need to log in
Let's go in order.
Here is the section of code responsible for the progress bar:
for i in range(lenLi):
sys.stdout.write('\r')
sys.stdout.write("[%-30s] %d%%" % ('='*i, i))
sys.stdout.flush()
part = float(i)/(lenLi-1)
symbols_num = int(30 * part)
for i in range(1, lenLi):
sys.stdout.write('\r')
part = float(i)/(lenLi-1)
symbols_num = int(30 * part)
sys.stdout.write("[%-30s] %3.2f%%" % ('='*symbols_num, part*100))
sys.stdout.flush()
for x in li:
. But, for the correct display of the progress bar, you need to read the step number. To do this, you can either create a variable that will count the steps, or use the enumerate function : Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question