Skip to content

Decremental While Loop #1164

Answered by casperdcl
GoodluckH asked this question in Q&A
May 4, 2021 · 1 comments · 1 reply
Discussion options

You must be logged in to vote
while h > 0:
    h /= 3

will technically be an infinite loop if it weren't for machine precision.

You could do:

with tqdm() as t:
    while h > 0:
        h /= 3
        t.update()

or if instead you mean:

while h > 1:
    h /= 3

then you could do:

import math
from tqdm import trange

total = int(math.ceil(math.log(h, 3)))
for _ in trange(total):
    if h <= 1:
        break
    h /= 3

Replies: 1 comment 1 reply

Comment options

casperdcl
Jun 22, 2021
Maintainer Sponsor

You must be logged in to vote
1 reply
@GoodluckH
Comment options

Answer selected by GoodluckH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants