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.
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.
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.
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?
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.
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.
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
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.
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.