我正在研究一個小概念驗證並使用python來說明這個想法。這個想法是程序將循環運行並檢查輸入。現在,如果輸入低於閾值,則會發送通知。但我試圖以4秒爲間隔限制通知。那就是我放棄邏輯或語法的地方。無論哪種方式它正在做一些意想不到的事檢查python中的條件後的重複性通知語句
1:繼續輸入0,它會顯示下面的閾值消息,直到它達到4秒標記,然後它只是在一行打印出消息4次。我希望他們每4秒後顯示一次。這個想法是(A)輸入可能在4秒內改變並且通知切換。 (B)我希望通知在每次腳本碰到條件時以4秒的重複次數進行提醒,如果weightIn < 0.5 ..如果它是真的,那麼通知從第一次起4秒後熄滅發送
對不起,如果我試着解釋它。我對python很新穎
import threading
def main():
while True:
weightIn = float(input("Get value: "))
threshold = .5
def operation():
if weightIn < 0.5:
#send notification at an interval of 4 sec
threading.Timer(4.0, operation).start()
print("Below weight threshhold...send notification")
else:
print("You are good")
if threshold is not None:
operation()
main()
對於時間,我會建議投票和timeit,這是應導入的模塊。 timeit是模塊,也就是說。輪詢是一個概念。 – 2014-10-27 05:39:22