2014-09-03 77 views
0

我正在教自己的Python,我試圖在特定時間打開CD驅動器的代碼。出於測試原因,我一直使用time.clock(),因爲它只使用數字,但我想使用time.ctime(),以便程序在特定時間工作。這是迄今爲止的代碼。有沒有更好的方法來循環代碼?

import time 
import ctypes 
if(time.clock()==10): 
    ctypes.windll.winmm.mciSendStringW("set cdaudio door open", 
      None, 0, None) 
for x in range(10**100): 
    print(x) 
    if(20>time.clock()>10): 
     ctypes.windll.winmm.mciSendStringW("set cdaudio door open", 
      None, 0, None) 
     quit() 

我使用的是打印(x)函數來監控代碼,我設置得足夠高,它不會達到的秒數門應打開之前停止的範圍內。

回答

0

相反的for x in range(10**100):你可以使用

while True: 
    # do stuff forever 

這個循環將繼續下去,直到你明確break出(或返回的功能,引發異常或退出程序等)。

time.ctime()返回一個字符串,這是不好比較,以給定的時間。相反,我會建議使用datetime模塊

go_time = datetime.datetime(2014, 9, 3, 12, 30) 
while True: 
    now = datetime.datetime.now() 
    if now >= go_time: 
     # open the draw etc... 
     quit() 
相關問題