This should be an error but it isn't


#1



I'm not stuck at an exercise but I noticed a bug. The section is called Hot Date.


The script I put in does not match what it should be, but it outputs as correct. I switched the hyphens (-) to forward slashes (/) but never changed the position of the day, month and year.


The code should have executed as bad syntax, good run.


from datetime import datetime
now = datetime.now()

print '%s/%s/%s' % (now.year, now.month, now.day)


#2

Actually this is not a bug it just means that the SCT was not as intricate as it should have been.


#3

@simms26 , this is interesting. Thanks for noticing it.

For many of the exercises, there is a single and unchanging correct output, and the SCT can check to make sure the user's code produces exactly that output. However, for the Python: Date and Time exercises, the actual date, and especially the time, can change while the code is executing. That might be one of the reasons why the author of this set of exercises chose to perform some format checking without checking the order of the values that are output.

The tests for the Date and Time series could actually be made more stringent regarding the order of the components of the output. However, the original Python track is scheduled to be replaced in the near future, so it is unlikely that the current SCT will be upgraded.

Incidentally, since the first exercise in this set, The datetime Library, has the user merely Click Save & Submit Code to continue., the test for that exercise is quite lenient. This leaves open the possibility for the user to experiment there with a wide variety enhanced solutions that can pass. Just for fun, here's a version of the code that writes a comment into itself.

import datetime
class DateTimeWriter(object):
    def __init__(self):
        self.now = datetime.datetime.now()
        with open("script.py", "a") as f:
            f.write("\n###### Submitted on {:02d}/{:02d}/{:04d} at {:02d}:{:02d}:{:02d} ######".format(self.now.month, self.now.day, self.now.year, self.now.hour, self.now.minute, self.now.second))

DateTimeWriter()

###### Submitted on 12/21/2016 at 13:27:30 ######

However, do not routinely write code like the above; code that modifies itself can be dangerous and prone to developing serious bugs.


#4