Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Party Invitations by freeman_lex
class Friend:
def __init__(self, name):
self.name = name
self.invited = "No party..."
def show_invite(self):
return self.invited
class Party:
def __init__(self, place):
self.place = place
self.observers = set()
def add_friend(self, obj):
self.observers.add(obj)
def del_friend(self, obj):
self.observers.discard(obj)
def send_invites(self, date):
for obj in self.observers:
obj.invited = self.place + ": " + date
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
party = Party("Midnight Pub")
nick = Friend("Nick")
john = Friend("John")
lucy = Friend("Lucy")
chuck = Friend("Chuck")
party.add_friend(nick)
party.add_friend(john)
party.add_friend(lucy)
party.send_invites("Friday, 9:00 PM")
party.del_friend(nick)
party.send_invites("Saturday, 10:00 AM")
party.add_friend(chuck)
assert john.show_invite() == "Midnight Pub: Saturday, 10:00 AM"
assert lucy.show_invite() == "Midnight Pub: Saturday, 10:00 AM"
assert nick.show_invite() == "Midnight Pub: Friday, 9:00 PM"
assert chuck.show_invite() == "No party..."
print("Coding complete? Let's try tests!")
April 6, 2023