Why doesn’t it accept my answer if I put spaces around the slashes?


#1

Question

Why doesn’t it accept my answer if I put spaces around the slashes?

Answer

For any exercise on Codecademy, your code is checked over by tests running in the background when you press Run. If the instructions ask for a specific output, variable name, or value, that is exactly what you must type.
Computer’s aren’t able to interpret what you meant to type, so we have to use exactly what is being checked for, otherwise it’ll be counted wrong.
In this exercise, it asks for us to display the date as mm/dd/yyyy, without any spacing anywhere. So that’s exactly how we should print it, and including spaces will throw a convenient error message:
It looks like there are spaces surrounding the slashes.


FAQ: Learn Python - Date and Time - Hot Date
#2

it did work

from datetime import datetime
now = datetime.now()
print '%02d / %02d / %04d' %(now.day, now.month, now.year)

my Result
08 / 09 / 2018


#3

Did it work in the lesson, though? What you have fashioned is print output, not a date string. Standard date formats have no spaces in them. In order for that to be accepted in the lesson, the spaces must be removed.


#4

take a look at my screenshot

i was wondering too sir


#5

I run it for hour:minute:second, it run and displayed the result but again displayed

It looks like there are spaces surrounding the colons. Remember to use + for string concatenation


Don’t whether its because i run it inside the practicing environment?
Sir, i really need clarification on this one.


#6

It is the lesson environment that is reporting that error which means it doesn’t match the expectations. The lesson checker is very limited in scope and does not have a range of expectations. More likely, there is one expected pattern to match against, and that’s it. Deviation from that pattern, however valid, will result in an error.

We’re writing lesson code, not real world production code. For best results follow the instructions to the letter.


#7

Thanks you very much sir.


#8

Does it matter what letter you use after the %02 ? I tried d and I and they worked but other like f and a didn’t. What is the importance of the letters after the %02/%04s?


#9

Yes, it must be a letter that the interpreter recognizes.

d => digit
i => integer (same as above)
f => float
s => string
r => representation (a list, tuple, set, dictionary)

%04s will pad the left side of a string with spaces up to a maximum output length of 4. If the string is 4 or more characters long there will be no pad.


>>> print ("%04s" % 'a')
   a
>>> print ("%04s" % 'abcde')
abcde
>>> print ("%04d" % 1)
0001
>>> print ("%4.2f" % 1.1)
1.10
>>> 

Note how it pads a number with 0’s. Note also how padding is ignored on floats.

https://pyformat.info/

The above has both old and new examples for comparison. The new format method may not work in Python 2. There is a way to include it but I’ve forgotten and will have to research that again.


#10

A post was split to a new topic: The result on console right but it doesnt check the box


#11

My output was right in my console changing the code to this form;
print ‘% 02d / % 02d / % 04d’ % ( now.month, now.day, now.year )
I had no error messages but this 11 / 19 / 2018


#12

What formatting instructions were you given. Be sure to follow that pattern exactly, with no spaces.

'%s/%s/%s' % ()

#13

I followed the instructions right and got it right and also did an experiment by going the wrong way and still had it right.


#14

Print the date and time together in the form:
mm/dd/yyyy hh:mm:ss

There is only one space character in the pattern.

Given that you passed on one attempt, any further attempts will not affect that result unless you Reset the exercise.