我正在制作一个代码来模拟一个棋子绕着垄断板转一百万次。我想有一个tqdm进度条,是更新时,每次扭转董事会是实现。
下面的是我当前的代码。,我使用的是一个which循环,当旋转板的次数超过期望的次数时,这个循环就停止了。
import os
from openpyxl import Workbook
from monopolyfct import *
def main(runs, fileOutput):
### EXCEL SETUP ###
theWorkbook = Workbook() # Creates the workbook interface.
defaultSheet = theWorkbook.active # Creates the used worksheet.
currentData = ["Current Table Turn", "Current Tile"] # Makes EXCEL column titles.
defaultSheet.append(currentData) # Appends column titles.
### CONTENT SETUP ###
currentData = [1, 0] # Sets starting position.
defaultSheet.append(currentData) # Appends starting position.
while currentData[0] <= runs:
### ROLLING THE DICES PROCESS ###
dices = twinDiceRoll()
currentData[1] += dices[2] # Updating the current tile
### SURPASSING THE NUMBER OF TILES ONBOARD ###
if currentData[1] > 37: # If more than a table turn is achieved,
currentData[0] += 1 # One more turn is registered
currentData[1] -= 38 # Update the tile to one coresponding to a board tile.
else:
pass
### APPENDING AQUIRED DATA ###
defaultSheet.append(currentData)
### MANAGIING SPECIAL TILES ###
if currentData[1] == 2 or 15 or 31: # Community chess
pass #TODO: Make a mechanic simulating the community chest card draw and it's related action.
elif currentData[1] == 5 or 20 or 34: # Chance
pass #TODO: Make a mechanic simulating the chance card draw and it's related action.
elif currentData[1] == 28: # Go to Jail
pass #TODO: Make a mechanic simulating the entire jail process
### TWIN DICE ROLL EXCEPTION ###
if dices[3] is True: # If the dices roll a double,
pass #TODO: Make a mechanic considering that three doubles sends one to Jail.
### STORING THE ACCUMULATED DATA ###
theWorkbook.save(fileOutput) # Compiles the data in a .xlxs file.
if __name__ == "__main__":
terminalWidth = os.get_terminal_size().columns # Gets current terminal width.
space(3)
print("Python Monopoly Statistics Renderer".upper().center(terminalWidth)) # Prints the title.
print("(PMSR)".center(terminalWidth)) # Prints the acronym.
space(2)
runs = int(request("For how many table turns do you want the simulation to run?")) # Prompts for the desired run ammount
#runs = 1000
fileOutput = request("What should be the name of the file in which statistics are stored?") # Prompts for the desired store filename
#fileOutput = "test"
fileOutput += ".xlsx" # Adds file extension to filename
main(runs, fileOutput)
发布于 2017-08-22 03:03:07
您可以在tqdm
中通过在构造函数中指定total
参数来使用手动控制。来自手册的逐字
with tqdm(total=100) as pbar:
for i in range(10):
sleep(0.1)
pbar.update(10)
更新
要手动控制没有上下文管理器的tqdm
(又名with
语句),您需要在使用完它之后关闭进度条。下面是手册中的另一个示例:
pbar = tqdm(total=100)
for i in range(10):
sleep(0.1)
pbar.update(10)
pbar.close()
为此,您需要知道预期运行的总数。在您的代码中,它看起来可能类似于
...
pbar = tqdm(total = runs+1)
while currentData[0] <= runs:
### ROLLING THE DICES PROCESS ###
dices = twinDiceRoll()
currentData[1] += dices[2] # Updating the current tile
### SURPASSING THE NUMBER OF TILES ONBOARD ###
if currentData[1] > 37: # If more than a table turn is achieved,
currentData[0] += 1 # One more turn is registered
currentData[1] -= 38 # Update the tile to one coresponding to a board tile.
pbar.update(1)
else:
pass
...
pbar.close()
然而,这段代码并不完美:如果currentData[1]
总是小于37,那么进度条就会停止而不是更新。如果试图在else:...
部件中更新它,可能会违反total
上限。这是第一步:)
发布于 2021-04-25 03:29:11
由于大家的关注,这篇文章吸引了我的注意,我想指出如何用无限的时间循环来实现这一点是很好的。
要使用带有tqdm的无限循环,需要使用生成器将while循环更改为无限for循环。
无限循环(无进度条)
while True:
# Do stuff here
无限循环(带有进度条)
def generator():
while True:
yield
for _ in tqdm(generator()):
# Do stuff here
上面的代码将创建一个不确定的进度条,类似于
16it [01:38, 6.18s/it]
请注意,还可以修改生成器以处理条件。
def generator():
while condition:
yield
发布于 2022-08-11 10:28:29
用户12128336的答案的附加版本。
您可以在生成器中执行所有的迭代操作。只需在迭代结束时添加yield
即可。
from tqdm.auto import tqdm
from time import sleep
def generator():
while True:
sleep(0.3) # iteration stuff
yield
for _ in tqdm(generator()): pass
# 77it [00:23, 3.25it/s]
https://stackoverflow.com/questions/45808140
复制相似问题