2017-09-27 89 views
0

所以我一直在玩一點點的時間表,我發現Schedule github有時間表,也非常好,易於使用。所以我迄今所做的是:Python - 格式/修剪,以便它給出正確的時間?

UserInput = input('To run Schedule script - Press y\nTo run directly - Press n\n') 

if(UserInput == 'n'): 
    main() 
elif(UserInput == 'y'): 
    TimeUser = input('What time to start script? Format - HH:MM ') 
    schedule.every().day.at(TimeUser).do(main) 
    wipe() 
    print('Schedule starts at: ' + TimeUser + ' - Waiting for time...') 

    while True: 
     schedule.run_pending() 
     time.sleep(1) 
     if(schedule.idle_seconds() == '5'): 
       print('Program starts in...:\n' + str(schedule.idle_seconds()) + '\n') 

但是什麼即時得到了現在的問題是,我的輸出結果是

程序啓動中...: 30.08442

Program starts in...: 
29.083967 

Program starts in...: 
28.083956 

Program starts in...: 
27.083923 

基本上我想要做的是if(schedule.idle_seconds()): 5秒。所以當它離開5秒鐘時,它應該開始打印出來。然而,我得到的問題是,由於毫秒我想,它不會達到5秒。所以我想知道是否有一種方法可以修剪/剪切/格式化它,以便它在5秒鐘後開始打印?

編輯OUTPUTT:

-------------------------------------- 
Schedule starts at: 13:55 - Waiting for time... 
-------------------------------------- 
Program starts in...: 
4.748427 

-------------------------------------- 
Wrong input - Try again 
-------------------------------------- 
To run Schedule task - Press y 
To run directly - Press n 

回答

1

因此,修剪浮動簡單的方法是:int(variable)但你可以做,沒有裝飾。試試這個:if(schedule.idle_seconds() < 5.0):而不是你的條件。但它將永無止境。如果你想停止循環工作,你必須創建附加條件。

例如:

while True: 
    schedule.run_pending() 
    time.sleep(1) 
    idle = schedule.idle_seconds() 
    if(idle < 5.0) and (idle >= 0.0): 
     print('Program starts in...:\n' + str(schedule.idle_seconds()) + '\n') 
+0

我認爲,即時通訊越來越奇怪錯誤。然而,當我嘗試運行這個,它確實說程序開始於:....但後來告訴我,我的輸入是錯誤的..那多奇怪..我編輯我的線程,讓你知道輸出 – WeInThis

+0

嗯...現在它的股價下跌至-0 ... ''' 計劃開始於...: 2.733921 計劃在開始...: 1.733449 計劃開始於...: 0.732948 計劃的開始...: -0.267551 -------------------------------------- 錯誤的輸入 - 再試一次 -------------------------------------- 要運行計劃任務 - 按y 要直接運行 - 按n ''' – WeInThis

+0

我認爲不是有'break'我可以使用main()。對? – WeInThis

1

該函數返回一個浮點數,所以它比較字符串不會有任何效果。 然而,你也許可以做線沿線的東西:

idle = int(round(schedule.idle_seconds())) 
if idle == 5: 
    print('Program starts in...:\n' + str(idle) + '\n') 

如果你想成爲安全起見,也可以落地的,而不是圓形:

idle = int(schedule.idle_seconds()) 
+0

這沒有奏效。所以當我嘗試改變爲''' idle = int(round(schedule。idle_seconds())) 而真: 打印(空閒) schedule.run_pending() time.sleep(1) idletwo = INT(schedule.idle_seconds()) 如果(空閒== '5'): print(「Test:」+ str(idle)+'\ n') ''' 它剛剛被卡住在同一秒鐘。所以如果還剩36秒。它只是打印出36 36 36 36 36 – WeInThis

+0

'如果空閒== 5'不等於'如果空閒=='5'',但是當你的問題解決了,無論如何,我想我們不需要進入這個-深度。 :) – Uvar

+0

我不這麼認爲!謝謝你。沒有你的「回合」我不會得到它! – WeInThis