0
我得到了下面的代碼。我已經添加了時間()來讓我計算用戶得到問題和用戶輸入數據之間的延遲。python3時間結果不正確
但由此產生的時間並不總是正確的,我不知道爲什麼。
from time import time
from random import randint
a = randint(0,1000)
b = randint(0,1000)
cor_answer = a+b
usr_answer = int(input("what is "+str(a)+"+"+str(b)+"? \n"))
start = time()
if usr_answer == cor_answer:
print("yes you are correct!")
else:
print("no you are wrong!")
end = time()
elapsed = end - start
print("That took you "+str(elapsed)+" seconds. \n")
這是CMDLINE執行結果:
~/math_quiz/math_quiz$ python3 math_quiz.py
what is 666+618?
1284
1284
1284
yes you are correct!
That took you 4.291534423828125e-05 seconds.
但時間()明確的作品,因爲如果我在怠速運轉,我得到這樣的:
>>> start = time()
>>> time()-start
13.856008052825928
所以我不確定爲什麼從cmdline執行得到不同的結果。
感謝
檢查答案後計時器。它是4.291534423828125e-05。它太小而無法在運行之間給出有意義的解決方案。 –