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

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.

1 Like

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

4 Likes

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.

take a look at my screenshot
datetime

i was wondering too sir

1 Like

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.

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.

2 Likes

Thanks you very much sir.

1 Like

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?

1 Like

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.

5 Likes

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

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

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

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

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

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.

1 Like

It will work, after we have completed the exercise.
If we did it, as per exercise, it does not matter later, if we change the output in same exercise; provided syntax is maintained correct.

Even if I set the year to two digits, it still printed out ‘2019’.

3 Likes

It will miss the check if you did it correctly right the first time. After doing it correctly I read the side readings and tried what someone mentioned with spaces. if you run it it will pass with no error. If you reset the lesson and do it with spaces it will not run and tell you to take out the spaces.

That is what happened when I was poking around this.

my one is showing 05/05/0005…
it should be 27/5/2020…
why it is happening?

Please post your code and a link to the this exercise.

Thanks Sir for your reply. It was my fault. I typed( now.month/ now.month/ now.month) which was wrong.

1 Like