2013-10-22 38 views
0

所以我想弄清楚這個學校。我試圖每分鐘打印一次,每十分鐘打印一次。到目前爲止,我不能每分鐘下載「打印x」。有人可以請幫忙。 這是我的代碼打印出每分鐘的信Python

import time; 
inTime = float(input("type in how many second")) 
oldTime = time.time()-inTime 


print (time.time()) 

def tenMin(oldTime): 
    newTime = time.time() 
    if ((newTime - oldTime)>= 25): 
     return True 
    else: 
     False 

while (True): 
     if (tenMin==True): 
      print ("x") 
      newTime = time.time() 
      oldtime = time.time() 
else: 
    oldTime = time.time() 
    continue 
+0

你試圖從一個輸入得到Min和十敏的數量或者你只是想1分鐘後打印一個軟件,10分鐘後打印一些東西? –

+0

我希望它能在一行中每十分鐘打印一次,而不是十分鐘後,週期重新開始 – kthao91

+0

感謝大家的幫助。如果你使用 「end ='' 它將打印在一行 和我的程序在else行上是錯誤的,我需要做的是刪除一些不需要的文本。但是我想感謝所有幫助我的人 – kthao91

回答

0

你的第一個問題是你比較函數引用到布爾線

if (tenMin==True): 

,明確的答案會是假的。你必須通過一個參數

if (tenMIn(oldTime)): 

...

+0

謝謝,但它似乎只打印一次,它似乎不像它的計數直到60打印出另一個 – kthao91

0

首先,你有一些問題與大家代碼:

  1. else: False - 這不是真正的Python語法。

  2. 如果你想定時器,你爲什麼要求用戶輸入?

  3. 你有一個邏輯問題:

    inTime = float(input("type in how many second"))

    oldTime = time.time()-inTime

    了time.time是浮動肯定的,但可以將用戶真正知道在UnixTime打印?

它不是最好的,但它的作品,我會提出一個簡單的解決方案。 它會打印出「X」每1分鐘,10分鐘後,將打印「\ n」(新行)

import time 

def main(): 

    #both timers are at the same start point 
    startTimerTen = time.time() 
    startTimerMin = startTimerTen 

    while True: 
     getCurrentTime = time.time() 
     if getCurrentTime - startTimerTen >= 600: 
      # restart both parameters 
      startTimerTen = getCurrentTime 
      startTimerMin = getCurrentTime 
      print "This in 10 min!\n" 
     if getCurrentTime - startTimerMin >= 60: 
      # restart only min parameter 
      startTimerMin = getCurrentTime 
      print "x" 


    #end of main 
if __name__ == "__main__": 
    main() 
+0

我們可以使用for循環,而不是while循環嗎? – kthao91

+0

你可以,但你需要一些條件,你有任何? –