私は、モノポリーのボード上でポーンが 100 万回回る様子をシミュレートするコードを作成しています。ボード上でポーンが 1 回転するたびに更新される tqdm 進行状況バーを用意したいと思います。
以下は私の現在のコードです。ボードの回転数が希望の数を超えると停止する while ループを使用しています。
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)
ベストアンサー1
コンストラクタに引数を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
。これは始まりです :)