-1

I want to have a progress bar starting before the while loop and ending with it as follows:

            a = [ '1', '2', '3', '4', '5', '6'] ## This can be 10, 15, 1000 elements 
            b = 5
            c = []
            timeout = time.time() + 60*b
            while True:
                for i in a:
                    if len(a) < 2:
                        print('Ok')
                    elif len(a) >= 2:
                        a.remove(i)
                        time.sleep(10)

                if len(a) == 2 :
                    print('Achieved results')
                    break
                elif time.time() > timeout:
                    print('Time ran out')
                    break
                else:
                    print('Still waiting calculations')
                    pass

I know my timeout is defined by a variable, but it is limited to something i will provide through this parameter, but how do i make a progress bar for all the time calculating this?

And also, how do i break the progress bar once the calculations are done as desired or if the time runs out?

Thank you

1 Answers1

0

A few problems with your current code before we get to solving it.

  1. the list a has strings '1','2', and '3'. However, you are adding then as an integer and checking for integer values.
  2. Also variable 'i' will be a string. so you cannot add integer 1 to i. it will fail.

Do you want to revisit the code please?

Unable to add comments as I dont have enough points to add comments so using answer section to provide you feedback.

Going into your answer, there is no easy way to provide a bar unless you use graphical interface. One way for you to show that you are progressing is to print out a string every time you go into the loop.

For example, you could have a while loop like this:

x = 0
while True:
    x +=1
    print ('=', end = '')
    if x > 20: break

The output will print a = every time the code goes through a loop. You can keep printing a string '=' or '.' every time you get into a process and get out of it. The end = '' will ensure the print statement stays in the same line. So it will eventually look like a bar.

That's the closest i could get to based on my limited knowledge of python.

Joe Ferndz
  • 7,732
  • 2
  • 7
  • 30
  • Hi, i've updated it! It's just that i need to understand how to implement it in a scenario like this where i got While > For > Ifs. Your help is much appreciated! – happymatei Jul 22 '20 at 00:26
  • does my new update with the while loop help you ? Is this something that will work for you? – Joe Ferndz Jul 22 '20 at 00:50
  • Hey, thanks for trying to help, but i can't apply that since i need the bar constantly on the screen and i want it to be delimited ( like everytime it ticks i see the progress with a start and an ending ). I was thinking this could be done because i have a specific timeout that will be defined all the time before the actual WHILE loop. Any ideas? Br – happymatei Jul 22 '20 at 00:57