2016-02-11 61 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