1

I am adding some code to the preset code to check the time availability, which is if the meeting time can fit into the proposed time schedule. However, I keep getting the following error. Can anyone please give me some advices? Thanks so much for your time.

Preset codes:

from datetime import datetime
class Meeting:
    def __init__(self, start_time, end_time):
        self.start_time = start_time
        self.end_time = end_time

My codes:

def check_availability(meetings, proposed_time): 
    meeting_start = Meeting.datetime.start_time.hour 
    meeting_end = Meeting.datetime.end_time.hour  
    ok_time = datetime.proposed_time.hour   
    if meeting_start < ok_time < meeting_end:
        return True 
    else:
        return False 

meetings = [Meeting(datetime(2018, 8, 1, 9, 0, 0), datetime(2018, 8, 1, 11, 
0, 0)), Meeting(datetime(2018, 8, 1, 15, 0, 0), datetime(2018, 8, 1, 16, 0, 
0)), Meeting(datetime(2018, 8, 2, 9, 0, 0), datetime(2018, 8, 2, 10, 0, 0))]

print(check_availability(meetings, datetime(2018, 8, 1, 12, 0, 0)))
print(check_availability(meetings, datetime(2018, 8, 1, 10, 0, 0)))
1
  • I have tried out the different codes, and it worked. However, it cannot pass some values. For example, meetings = [Meeting(2018-08-03 13:00:00, 2018-08-03 15:45:00)] . When I tried the above values on pythontutor, it gave the SyntaxError: invalid token. How to get around with it? Thanks so much! Commented Jan 28, 2019 at 12:19

1 Answer 1

2

Your code raises this exception:

AttributeError: type object 'Meeting' has no attribute 'datetime'

At this line:

meeting_start = Meeting.datetime.start_time.hour

Python is telling you that the Meeting class doesn't have an attribute named datetime. This is true: the Meeting class is a factory for making meeting objects (or instances), and these objects have start_time and end_time attributes, which are set by passing datetime instances to Meeting's __init__ method. These attributes can be accessed like this:

>>> meeting = Meeting(datetime(2018, 8, 1, 9, 0, 0), datetime(2018, 8, 1, 11, 
0, 0))
>>> print(meeting.start_time)
2018-08-01 09:00:00                                                                                                     
>>> print(meeting.end_time)                                                                                             
2018-08-01 11:00:00 

Your check_availability function is being passed a list of meetings, so you need to loop over the list to check whether any of the meetings conflict with the proposed meeting time.

def check_availability(meetings, proposed_time):
    # Loop over the list of meetings; "meeting"
    # is the meeting that you are currently inspecting.
    for meeting in meetings:
        # if proposed_time is between meeting.start_time
        # and meeting.end_time, return False
    # If you get through the list without returning False
    # then the proposed time must be ok, so return True.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.