我见过tqdm在for循环中显示了进展,但我想知道如何使用while循环来实现这一点。我在这里有一些代码,可以得到硬币翻转在任意数量的翻转中的百分比:
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
该代码实质上是接受用户的输入并多次抛硬币,然后给出正面和反面的百分比。
当这种情况发生时,我希望有一个进度条,但是我不知道如何使用带While循环的tqdm。
有人知道怎么做或者有其他选择吗?谢谢。
编辑:顺便说一下,这段代码还有更多内容,但我决定不添加它,因为我认为它无关紧要。
发布于 2020-05-21 18:04:43
正如@r.ook所说:如何决定无限循环的进程?它要么是循环,要么不是。即0%或100%。
因此,tqdm只适用于for循环。
解决办法:
for i in tqdm(range(flips)):
这样,即使事先不知道迭代次数,它仍将显示一个进度条。
https://stackoverflow.com/questions/61940595
复制相似问题