### 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: ### 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)
1 个评论
如果你提前知道 runs 的值,为什么不直接使用for-loop?
python
python-3.x
tqdm
Benjamin Chausse
Benjamin Chausse
发布于 2017-08-22
3 个回答
RafazZ
RafazZ
发布于 2022-08-11
已采纳
0 人赞同

你可以在 tqdm 中通过在构造函数中指定一个 total 参数来使用手动控制。逐字逐句地从 手册 :

with tqdm(total=100) as pbar:
    for i in range(10):
        sleep(0.1)
        pbar.update(10)

To 手册ly control the tqdm without the context manager (aka with statement), you will need to close the progress bar after you are done using it. Here is another example from the 手册:

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: pbar.close()

然而,这段代码并不完美:考虑到如果currentData[1]总是小于37 -- 进度条会直接停止而不更新。如果你试图在else:...部分更新它,你可能会违反total的上界。这只是一个开始 :)

要使上述代码工作,你需要 from tqdm import tqdm pbar = tqdm.tqdm(total=100)
更好的做法是使用 with tqdm(total=100) as pbar: do... ; pbar.update(10)
非常有用,谢谢!
Why sleep is needed?
请注意, pbar.update(...) 的参数是对 increment 进度条,而不是将其设置为绝对值。
user12128336
发布于 2022-08-11
0 人赞同

因为这个帖子吸引了很多人的注意,我想应该指出如何用一个无限的while循环来实现这一点,也是不错的。

要使用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
    
这确实是一个很好的解决方案。
这很好,但由于某些原因 RuntimeError: cannot join current thread 在最后。有谁知道如何处理这个问题吗?
Ivan Reshetnikov
Ivan Reshetnikov
发布于 2022-08-11
0 人赞同

为用户12128336的回答增加一个版本。

你可以在生成器中做所有迭代的事情。只要在迭代结束时加上 yield 即可。

from tqdm.auto import tqdm
from time import sleep
def generator():
    while True:
        sleep(0.3) # iteration stuff
        yield