[英]how do I make a Timer in Python
如何在 python 中創建計時器? 我的項目是一個速度打字測試,計時器用來計時用戶打字的長度。 用戶鍵入的第一個任務是字母表,盡可能快,然后第二個任務是再次以盡可能快的速度再次鍵入一組以隨機順序排列的單詞
時間模塊允許用戶直接獲取自 1970 年以來的時間,以秒為單位(參見: https://docs.python.org/3/library/time.ZFC35FDC70D5FC69D269883A822C7A53 )。 這意味着我們可以從之后的時間中減去之前的時間,看看已經過了多長時間,即用戶完成打字測試用了多長時間。 從那里開始,就像打印結果一樣簡單。 您可以使用int
對時間進行舍入,以獲得沒有毫秒的純秒結果。
# Import the time library
import time
# Calculate the start time
start = time.time()
# Code here
# Calculate the end time and time taken
end = time.time()
length = start - end
# Show the results : this can be altered however you like
print("It took", start-end, "seconds!")
您可以使用及時構建庫:
import time
strToType="The cat is catching a mouse."
start_time = time.perf_counter()
print("Type: '"+strToType+"'.")
typedstring=input()
if typedstring==strToType:
end_time = time.perf_counter()
run_time = end_time - start_time
print("You typed '"+strToType+"' in "+str(run_time)+" seconds.")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.