我試圖持續運行一個WHILE循環來檢查每十五分鐘的情況。當使用time.sleep(900)時,它會首先執行WHILE循環15分鐘,然後在條件滿足時停止運行。在Python 3.3中的time.sleep()函數?
我相信Python 2因爲這個原因使用了這個函數,Python 3.3不會跟着這個嗎?如果不是,即使條件已滿足,我將如何無限期地通過while循環運行?
下面是我的代碼片段目前:
if price_now == 'Y':
print(get_price())
else:
price = "99.99"
while price > "7.74":
price = get_price()
time.sleep(5)
編輯:更新基於eandersson反饋。
if price_now == 'Y':
print(get_price())
else:
price = 99.99
while price > 7.74:
price = get_price()
time.sleep(5)
的get_price()
功能:
def get_price():
page = urllib.request.urlopen("link redacted")
text = page.read().decode("utf8")
where = text.find('>$')
start_of_price = where + 2
end_of_price = start_of_price + 4
price = float(text[start_of_price:end_of_price])
return(price)
更新您的問題,包括原代碼,以防止任何混淆。 :) – eandersson 2013-03-25 02:16:30