1

I have seen tqdm used to show progress in for loops, but I was wondering how to do that with while loops. I have some code here to get the percentages of coin flips at any number of flips:

def flipem():
    global flips, completed_flips, heads, tails, head_amount, tail_amount, total_amount
    while completed_flips != flips:
        flip = randint(1, 2)
        if flip == 1:
            head_amount += 1

        elif flip == 2:
            tail_amount += 1

        else:
            pass

        completed_flips += 1
        total_amount += 1


    if completed_flips == flips:
        global head_percentage, tail_percentage
        head_percentage = head_amount / total_amount * 100
        tail_percentage = tail_amount / total_amount * 100

That code essentially takes a user's input and flips a coin that many times, then gives the percentages of heads and tails.

While this is happening I would like there to be a progress bar but I can't figure out how to use tqdm with a while loop.

Anybody know how or have an alternative? Thanks.

EDIT: Btw, there is more to this code but I decided not to add it because I think it is irrelevant.

Sam_Cameron
  • 105
  • 2
  • 11
  • 1
    How does one determine the progress of an infinite loop? It either is looping, or *isn't*. i.e. 0% or 100%. – r.ook May 21 '20 at 17:46
  • @r.ook That is a very good point. – Sam_Cameron May 21 '20 at 17:47
  • @r.ook does this mean I would have to know the amount of flips before hand so I would have to use a for loop? – Sam_Cameron May 21 '20 at 17:48
  • 1
    In your case, since you *do* know how many `flips` you want to reach, wrap `tqdm` around `for flip in range(len(flips)): ...` – r.ook May 21 '20 at 17:53
  • @r.ook I figured it out. Thank you for showing me how dumb I am lol. – Sam_Cameron May 21 '20 at 17:54
  • 1
    @r.ook I figured out how to do it without knowing the amount of flips before hand: ```for i in tqdm(range(0, flips):``` – Sam_Cameron May 21 '20 at 17:55
  • 1
    I would recommend you adding your comment as an answer to your own question, it would greatly benefit the future visitors when they happen upon your question. I won't want to steal your thunder as you arrived at the conclusion yourself. Get some upvotes :) – r.ook May 21 '20 at 17:59
  • @r.ook I will. Thanks – Sam_Cameron May 21 '20 at 18:01

1 Answers1

0

As @r.ook said: How does one determine the progress of an infinite loop? It either is looping, or isn't. i.e. 0% or 100%.

SO, tqdm only works with for loops.

The solution:

for i in tqdm(range(flips)):

This way, even without knowing the number of iterations beforehand, it will still show a progress bar.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Sam_Cameron
  • 105
  • 2
  • 11
  • 1
    Uhm, I think that this answer is misleading. You can use tqdm with a while loop, as long as you set an upper bound with your iterations. Check https://stackoverflow.com/a/45808255/4240413, which is the answer to a question practically identical to the one here. That solution can be adapted to your case I think, being flips the max number of iterations. – Davide Fiocco Dec 25 '20 at 20:12