-3

I've just started to learn to code, python specifically. I am taking the classes on codeacademy, and had some limited knowledge from years ago before doing so. I am up to like the 8 or 9th class on there.

I decided I would some of things to use and code in my shell on my local PC instead of there's online. I want to write the date and display it to the user, however no matter what I do the code I am trying to use displays a syntax error, or unsupported type error. Any help would be much appreciated. This code seems work fine on the codeacademy website. I am running the newest version of python 3.4

from datetime import datetime

now = datetime.now()

print '%s/%s/%s %s:%s:%s' % (now.month, now.day, now.year, now.hour, now.minute, now.second)
ydaetskcoR
  • 53,225
  • 8
  • 158
  • 177
Christopher Nelson
  • 887
  • 2
  • 17
  • 26
  • 1
    You should really show the error you're getting. In this case however you need to use Python 3 syntax for `print`ing rather than Codecademy's Python 2 syntax. – ydaetskcoR Dec 27 '14 at 13:19
  • @ydaetskcoR thats true. `%s` is deprecated –  Dec 27 '14 at 13:19
  • you now have to use `{0}.format(variable)` –  Dec 27 '14 at 13:20
  • More importantly, print is a function and so should have everything wrapped in parentheses in Python3 – ydaetskcoR Dec 27 '14 at 13:20
  • 1
    Please do not spread FUD; `%s` [is not deprecated](https://mail.python.org/pipermail/python-dev/2014-January/131799.html), and will never be. It's a completely legitimate way of formatting strings in Python. – user4815162342 Dec 27 '14 at 13:27
  • @JuanRocamonde They dropped the plan of deprecating `printf()` style formatting a while ago. – Ashwini Chaudhary Dec 27 '14 at 13:30
  • You should use the `print()` function and format strings: `python3 -c 'import datetime; print("{:%m/%d/%Y %H:%M%S}".format(datetime.datetime.now()))'` (and if you print the hours-minutes-seconds from big to small you should consider using year-month-day for consistency as well). – Anthon Dec 27 '14 at 13:31
  • all right I am just saying what I read, that you should use .format() instead of %s. –  Dec 27 '14 at 13:32

2 Answers2

1

it print: the syntax of print is different in python 2x and 3x. in code accedmy, they are using python 2x, so its working there, but you using python 3x, for that you need the brackets

print('%s/%s/%s %s:%s:%s' % (now.month, now.day, now.year, now.hour, now.minute, now.second))

you need to use bracket

better to use format:

print('{}/{}/{} {}:{}:{}'.format(now.month, now.day, now.year, now.hour, now.minute, now.second))
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
  • True! By the way, %s is deprecated in Python 3.x. Now it is preferred the .format function –  Dec 27 '14 at 13:21
  • % is deprecated in Python 3 and instead the OP should use the format method instead. Also you should explain why they need to use the parentheses here but not on Codecademy. – ydaetskcoR Dec 27 '14 at 13:21
  • 1
    Better use `now.strftime(%m/%d/%Y %H:%M:%S)` rather than pull out each component separately.. The `datetime.__format__()` hook method supports the same formatting, so you can use `'The current date and time is: {:%m/%d/%Y %H:%M:%S}'.format(now)`. – Martijn Pieters Dec 27 '14 at 13:26
  • @MartijnPieters yes but i was just making code work. and want to correct OP, so he can understand wts wrong – Hackaholic Dec 27 '14 at 13:27
  • This was very helpful. Thank you. Stinks I have been using codeacademy only to find that some of the code is outdated. However, it seems this was a great place to pose the question. – Christopher Nelson Dec 27 '14 at 13:31
  • Ff you use format then use it right and apply the `datetime` formatting parameters: `print("{:%m/%d/%Y %H:%M%S}".format(datetime.datetime.now())` instead of having multiple `{}` entries – Anthon Dec 27 '14 at 13:35
1

You may consider using strftime method from datetime:

now.strftime('%m/%d/%Y %H:%M:%S')
# this will give you a format string as you want
'12/27/2014 13:18:59'
Anzel
  • 19,825
  • 5
  • 51
  • 52
  • 1
    That does not actually answer the question even though it is good to point out. –  Dec 27 '14 at 13:22
  • true, it should really go to comment section. – Anzel Dec 27 '14 at 13:23
  • Why would you want ever want to use `strftime()` if you are going print anyway just put the format in the format string handed to `print()` – Anthon Dec 27 '14 at 13:30
  • @Anthon, you're right one should use print directly, I'm only pointing out `strftime` is a method worths considering using – Anzel Dec 27 '14 at 13:33
  • And I think Martijn Pieters's comment on another post should the the correct & accepted answer, that's why I am not bother changing my answer – Anzel Dec 27 '14 at 13:35