Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #44640

Re: help to code...

From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: help to code...
Date 2013-05-02 17:08 +0100
References <1F2E5209-2F0F-4DE2-88BC-DB9C2267B0F3@icloud.com> <CAPTjJmpvAmG_wFnz-6OWELi6C8cYj-2Cgmn_GyR=BsC4=aD1SQ@mail.gmail.com> <klu0io$vss$1@ger.gmane.org> <51828822.5080102@mrabarnett.plus.com>
Newsgroups comp.lang.python
Message-ID <mailman.1252.1367510950.3114.python-list@python.org> (permalink)

Show all headers | View raw


On 02/05/2013 16:37, MRAB wrote:
> On 02/05/2013 16:26, Mark Lawrence wrote:
>> On 02/05/2013 15:59, Chris Angelico wrote:
>>> On Thu, May 2, 2013 at 11:50 PM, leonardo selmi <l.selmi@icloud.com>
>>> wrote:
>>>> dear python community,
>>>>
>>>> i wrote the following program:
>>>>
>>>> print str(current_month) + '/' + str(current_day) + '/' +
>>>> str(current_year)
>>>> +' '+
>>>> print str(current_hour) + str(current_minute) + str(current_second)
>>>>
>>>> SyntaxError: invalid syntax
>>>>
>>>> how can i write the last two lines correctly?
>>>
>>> You're doing two separate print statements. Either join them into one
>>> (if you want it to be one line), or drop the last + on the first line,
>>> which is causing your syntax error. But there's an even easier way to
>>> do this: Use formatted printing.
>>>
>>> print("%d/%d/%d
>>> %d%d%d"%(current_month,current_day,current_year,current_hour,current_minute,current_second))
>>>
>>>
>>> Or, since you're getting those straight from 'now':
>>>
>>> print("%d/%d/%d
>>> %d%d%d"%(now.month,now.day,now.year,now.hour,now.minute,now.second))
>>>
>>> I strongly suspect that you want to put delimiters in the time, though
>>> (colons, perhaps?). It'd be really nice, by the way, if you'd avoid
>>> the messy American format date with the month first; put the year
>>> first and it's unambiguous!
>>>
>>> ChrisA
>>>
>>
>> Better IMHO is to use strftime
>> http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
>>
>> so the complete code could be
>>
>> from datetime import datetime
>> print(datetime.now().strftime('%m/%Y/%d %H %m %S'))
>>
> Except, of course, putting the year first, and using "%m" for the
> minutes:
>
> print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
>
> :-)
>

Congratulations on spotting my deliberate mistake :)

-- 
If you're using GoogleCrap™ please read this 
http://wiki.python.org/moin/GoogleGroupsPython.

Mark Lawrence

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: help to code... Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-05-02 17:08 +0100

csiph-web