Date and time confusion


#1


https://www.codecademy.com/courses/python-beginner-en-zFPOx/0/6?curriculum_id=4f89dab3d788890003000096#

Your code looks a bit off. Check the Hint if you need help! Your
code threw the following error: invalid syntax (python, line 5)
.>


from datetime import datetime
now = datetime.now()

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


#2

Exercise 6 wants you to print the date and time out on one line.

7/17/2016 8:29:26


#3

thats what i need help with i know im supposed to do that but i don't know how


#4

@xxserpentsxxyt printing your code like this:

Will give you the output like this:

7/18/2016
1:23:38

Do you see how writing the values on two separate lines gives you the answer back on two separate lines as well? If you write everything on one line, it will print it back on one line as well, so your code should look line this:

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)

Do you understand why you had to write it like that? Let me know if you didn't or need more help.

Good luck and happy coding! :slight_smile:

-- @chipjumper36731


#5

I wrote the code like that and it combined the year with the hour instead of having a space in between.

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

which gives me this 7/18/201622:12:35

instead of this 7/18/2016 22:12:35


#6

Nevermind, i got it to work


#7

Okay :slight_smile: Keep coding!


#8

interesting side note, without the space before now.month my code didn't work, it seems that without it the now.minute wrapped as now on one line and .minute on the next causing it to fail


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.