2016-02-11 60 views
-1

所以我有一個變量,需要顯示爲一天的時間,即3:02,然後使計算關閉該變量,即將7添加到第一個變量,惠靈頓,並顯示爲3:09。 這是一個簡單的問題,它不需要進入日期時間 有沒有一種方法可以做到這一點,沒有日期?試圖顯示一個變量作爲打印時間

def traintimes(): 
    crofton = wellington + 7 
    ngaio = crofton + 2 
    awarua = ngaio + 2 
    simla = awarua + 2 
    boxhill = simla + 1 
    kandallah = boxhill + 2 
    raroa = kandallah + 2 
    johnsonville = raroa + 2 
    print ("Wellington:",wellington,"Crofton Downs:",crofton,"Ngaio:",ngaio,"Awarua Street:",awarua,"Simla Crescent:",simla,"Box Hill:",boxhill,"Kandallah:",kandallah,"Raroa:",raroa,"Johnsonville:",johnsonville) 
wellington = int("3:02") 
traintimes() 
wellington = int("3:28") 
traintimes() 
wellington = int("3:40") 
traintimes() 
+1

可能重複[在Python中爲datetime.time添加N秒的標準方式是什麼?](http://stackoverflow.com/questions/100210/what-is-the-standard-way-to-add -n-seconds-to-datetime-time-in-python) –

回答

0

你想使用datetime模塊,而不是使用整數。使用timedelta進行計算。

例子:

​​

隨着時間的工作是非常困難的,而不正確的庫,我建議採取閱讀datetime文檔的時間。

另外,this article是非常需要閱讀所有與時間一起工作的程序員。

+0

這會讓我把它設置爲一天中的某個時間嗎?就像只有3:02或2:15一樣嗎? – Nash