Question: Why do I keep getting this error? The call daytime(datetime.datetime(2015, 6, 5, 7, 0),daycycle) returns None, not True. def daytime(time,daycycle): Returns true if the
Why do I keep getting this error? "The call daytime(datetime.datetime(2015, 6, 5, 7, 0),daycycle) returns None, not True."
def daytime(time,daycycle): """ Returns true if the time takes place during the day. A time is during the day if it is after sunrise but before sunset, as indicated by the daycycle dicitionary. A daycycle dictionary has keys for several years (as int). The value for each year is also a dictionary, taking strings of the form 'mm-dd'. The value for that key is a THIRD dictionary, with two keys "sunrise" and "sunset". The value for each of those two keys is a string in 24-hour time format. For example, here is what part of a daycycle dictionary might look like: "2015": { "01-01": { "sunrise": "07:35", "sunset": "16:44" }, "01-02": { "sunrise": "07:36", "sunset": "16:45" }, ... } In addition, the daycycle dictionary has a key 'timezone' that expresses the timezone as a string. This function uses that timezone when constructing datetime objects from this set. If the time parameter does not have a timezone, we assume that it is in the same timezone as the daycycle dictionary Parameter time: The time to check Precondition: time is a datetime object Parameter daycycle: The daycycle dictionary Precondition: daycycle is a valid daycycle dictionary, as described above """ # HINT: Use the code from the previous exercise to get sunset AND sunrise # Add a timezone to time if one is missing (the one from the daycycle)
try:
tz_cycle = daycycle['timezone']
except KeyError:
return False
if time.tzinfo is not None:
year1 = time.strftime('%Y')
date1 = time.strftime('%m-%d')
try:
sunrisetime = daycycle[year1][date1]['sunrise']
sunsettime = daycycle[year1][date1]['sunset']
except KeyError:
return False
y = year1 + '-' + date1
y1 = datetime.datetime.strptime(y, '%Y-%m-%d')
y3 = datetime.datetime.strptime(sunrisetime, '%H:%M').time()
y2 = datetime.datetime.combine(y1, y3)
x = y2.isoformat()
y4 = datetime.datetime.strptime(sunsettime, '%H:%M').time()
y5 = datetime.datetime.combine(y1, y4)
x1 = y5.isoformat()
sunrise = str_to_time(x, tz_cycle)
sunset = str_to_time(x1, tz_cycle)
if sunrise < time < sunset:
return True
else:
return False
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
