2013-12-14 117 views
1

因此,使用该网站,我想出了下面的代码:显示时间

import time 
print(time.strftime('%l:%M%p %Z on %b %d %Y')) 
#' 1:36PM EDT on Oct 18, 2010' 
print("time done") 
time.sleep(5) 

从下列来源:12

出于某种原因,不过,这个代码不工作。

我试图让时间显示类似的评论,但如果我可以得到秒,以及这将是好事。 我也试过这个:

import time 
print time.strftime("%l"+":"+"%M"+"%p"+ "%Z"+ "%b"+ "%d"+ "%Y") 
#' 1:36PM EDT on Oct 18, 2010' 
print("time done") 
time.sleep(5) 

但它也不起作用。

编辑:错误,我从print(time.strftime('%l:%M:%S%p %Z on %b %d, %Y'))报复:

AttributeError: '<invalid type>' object has no attribute 'strftime' 

EDIT2:对于那些不相信我:在CodeSkulptor该错误以及代码https://www.dropbox.com/s/joyxsod8yv899fm/THERE.png

我用python的3.3.3和codeskulptor

+0

连接组件依然构建完全相同的字符串格式,因此使得* no *不同。 –

+0

你的错误将*不*有'<无效类型>'那里;它将是'AttributeError:'str'对象没有属性'strftime'或'AttributeError:'builtin_function_or_method'对象没有属性'strftime'或类似的。你很可能会把时间反弹到别的东西上。 –

回答

1

所有你忘了%d后一个逗号:

>>> print(time.strftime('%l:%M%p %Z on %b %d, %Y')) 
7:06PM GMT on Dec 14, 2013 

添加在几秒钟意味着你要插入%S地方:

>>> print(time.strftime('%l:%M:%S%p %Z on %b %d, %Y')) 
7:07:47PM GMT on Dec 14, 2013 

注意CodeSkulptor不支持time模块非常好。在CodeSkulptor上使用time.strftime()的代码将失败,但这不是Python的问题。即使基地print(time.strftime())呼叫失败。

+0

另外,作者在第一次打印时忘记了括号(他使用的是Python 3.x) –

+0

仍然出现错误,请参阅编辑问题 – L0neGamer

+1

@ L0neGamer:您将'time'重新设置为另一个值。 'time = ...'替换模块。 –