2017-08-29 65 views
0

我正在研究一個非常基本的Python骰子滾動程序,目前我正在添加一個ETA系統(要求程序滾動骰子100多次以上需要一段時間,有些人可能會將其視爲崩潰),系統我想到的是以下內容:如何在不凍結程序的情況下計算變量的變化? [Python]

由於「骰子」是通過生成一個隨機數並在for循環中重複「滾動」,如果我接受該變量並在一秒鐘後將其與變量進行比較,我可以做一些基本的數學來猜測剩下的時間。

我的問題是我如何可以等待秒之間沒有完全凍結程序(time.sleep)。

任何幫助將不勝感激,謝謝!

CODE:

import random 
finished = 0 
printresults = 0 
dicesides = 0 
rolls = 0 
amountcompleted = 0 
while finished !="n": 
    finished = 0 
    printresults = 0 
    dicesides = 0 
    rolls = 0 
    amountcompleted = 0 
    rollsdone = 0 
    countlist = [] 

    rolls = int(input("How many times should the dice be rolled? ")) #Variable that counts how many times the dice should be rolled 

    dicesides = int(input("How many sides does the dice have? ")) #Variable that contains how many sides the dice has 

    while printresults !="y" and printresults !="n": 
     printresults = input("Print dice roll results as they're created? say y/n ") #If user says y, result will be printed as results are made 
     if printresults !="y" and printresults !="n": 
      print("Answer invalid") 

    while amountcompleted !="y" and amountcompleted !="n": 
     amountcompleted = input("Print the amount of rolls completed as the dice is rolled? (Reccomended when rolling over 1M times) answer y/n ") 
     if amountcompleted !="y" and amountcompleted !="n": 
      print("Answer invalid") 

    for counter in range(0,dicesides): #Creates list of the right length to hold results 
     countlist.append(0) 

    for counter in range (0,rolls): #Main bit of the script that actually calculates and stores amount of dice rolls 
     number = random.randint(1,dicesides) #"Rolls" the dice, using the dicesides variable. 
     if printresults == "y": 
      print(number) #Prints the results as they are made if enabled 
     if amountcompleted == "y": 
      (rollsdone) = int(rollsdone + 1) 
      print("Completed {0} rolls".format((rollsdone))) 
     for counter in range (0,dicesides + 1): #For variable to store the results in a list 
      if number == counter: 
       countlist[counter-1] = countlist[counter-1] + 1 #Adds 1 to the right bit of the list 

    for counter in range(0,dicesides): 
     print("Number of {0}s: {1}".format(counter + 1,countlist[counter])) #Prints results 
    while finished != "y" and finished != "n": 
     finished = input("Reroll? Answer y/n ") #Asks the user if they want to reroll with different settings 
     if finished != "y" and finished != "n": 
      print("Input invalid") 
+0

你想要什麼?你想要時間流逝嗎? –

+2

顯示代碼。如果你想在計數之間等待一秒鐘,並且有1000000+骰子擲出,那麼這就是1000000+秒吧? –

回答

0
import time, random 

def roll(n): 
    timeStarted = time.time() 
    recorded = False 
    for x in range(1, n + 1, 1): 
     random.randint(1, 6) 

     now = time.time() 

     if (int(now) - int(timeStarted) == 1) and not (recorded): 
      recorded = True 
      rollsSecond = n - x 
      print(rollsSecond, "rolls per second") 

roll(10000000) 

支票上開始的時間和比較晚一秒鐘

+0

看起來不錯,會加入它並看看它是如何工作的(並且如果它運作良好,請添加它作爲答案:D) – UnicornsOnLSD

+1

謝謝!這使我朝着正確的方向發展:D – UnicornsOnLSD

0

如果使用這樣的事情你們之間的時間差,

>>>import time 
>>> s = time.time() 
>>> for i in range(10): 
... print i 
... 
0 
1 
2 
3 
4 
5 
6 
7 
8 
9 
>>>end = time.time()-s 
>>> print end 
15.814720153808594 
>>> 

這裏s是開始時間。一旦過程完成,將會得到不同的結果......這是end

0

只是爲了貢獻。您可以輕鬆創建裝飾器並在整個應用程序中使用它。這裏有一個例子:

class TimeMuncher(object): 
    def __init__(self, f): 
    self.start = None 
    self.stop = None 
    self.function = f 

    def __call__(self, *args): 
    self.start = time.time() * 1000.0 
    self.function(*args) 
    self.stop = time.time() * 1000.0 
    self.tell_time() 

    def tell_time(self): 
    #or log.. 
    print("{} : {} ms".format(self.function.__name__, self.stop - 
    self.start)) 

@TimeMuncher 
def aFunction(name, no): 
    print("my function " + name + str(no)) 

aFunction("hello", 2) 

所以每次調用某個函數或方法時,都可以管理其工作所需的時間。感謝python OO,它很容易定製。

編輯:我認爲你應該重構你的代碼,這樣你可以評估你的程序的特定邏輯的時間,而不是你正在使用的當前巨大的功能。編輯2:您可以在TimeMuncher上設置CURRENT_TIME/WAIT_TIME靜態變量,並通過編輯__init__()__call__()以給定時間間隔記錄/打印您的時間。

相關問題