class PhoneNumber: """ A PhoneNumber represents a single phone number made up of the area code, exchange, and the line number. EX: (415) 552-7909 ^ ^ ^ | | | | | number | exchange area code """ def __init__(self, area_code, exchange, number): """ Creates a new PhoneNumber from the provided area code, exchange and number. """ self.area_code = area_code self.exchange = exchange self.number = number def call(self): """ Calls this PhoneNumber. """ print "Calling (" + str(self.area_code) + ") " + str(self.exchange) \ + "-" + str(self.number) print "ring... ring... Hello?" def print_number(self): """ Prints a pretty version of this PhoneNumber. """ print "(" + str(self.area_code) + ") " + str(self.exchange) \ + "-" + str(self.number) class PhoneBook: """ A PhoneBook is a collection of names and phone numbers. """ def __init__(self): """ Creates a new PhoneBook that is initially empty. """ self.contacts = {} def add_number(self, name, phone_number): """ Adds the provided name and PhoneNumber to this PhoneBook. Will replace the number if the name already exists in this PhoneBook. """ self.contacts[name] = phone_number def delete_contact(self, name): """ Removes the provided name and the associated PhoneNumber from this PhoneBook. """ # This is how to remove from a dict. We might not have used this before. del self.contacts[name] def call(self, name): """ Calls the phone number associated with the provided name. """ self.contacts[name].call() print "Hi this is " + name + "." def get_phone_number(self, name): """ Returns the PhoneNumber associated with the provided name. """ return self.contacts[name] def get_contacts_in_area_code(self, area_code): """ Returns a list of all PhoneNumbers in this PhoneBook that have the given area_code. """ result = [] for name in self.contacts: num = self.contacts[name] if num.area_code == area_code: result.append(num) return result