Create a class CitiesAndCountries with at least three methods: class CitiesAndCountries: def add_country(self, country_name): """ :param country_name: name of new country :return: True if added, False if duplicate """ def add_city(self, country_name, city_name): """ :param country_name: must be a country in the already defined countries :param city_name: name of new city. :return: True if added, False if not added """ def find_city(self, city_name): """ :param city_name: get all cities with that name, in all different countries :return: list of countries which have a city with that name, empty list if none """

Respuesta :

Answer:

Check the explanation

Explanation:

class CitiesAndCountries:

   """

   This is the class which maintains a dictionary of the Countries with the cites in them.

   """

   def __init__(self):

       """

       Constructor for the class CitiesAndCountries.

       It defines a dictionary to store the names of the countries and a list of the cities which are in them

       Example of the the following dictionary when it is populated:

       citiesandcountries = {"india":["bhopal", "mumbai", "nagpur"], "usa":["new york", "seatlle"]

       Keeping the names in lowercase helps in search

       """

       self.citiesandcountries = {}  # This is the dictionary which stores the information

   def add_country(self, country_name):

       """

       The add_country function to add countries to our list.

       :param country_name: Name of the country to add to the dictionary

       :return: True if added to the dictionary, False if it is already present in the dictionary

       """

       if country_name.lower() not in self.citiesandcountries.keys():

           self.citiesandcountries[country_name.lower()] = [] # Storing the country name in lower case helps in

           # searching

           return True

       else:  # You could omit the else statement and directly return False, but it adds to readability.

           return False

   def add_city(self, country_name, city_name):

       """

       This function adds the cities names to the dictionary corresponding to their countries.

       :param country_name: Country to which the city belongs

       :param city_name:  Name of the city to be added

       :return: True if the country is present in the dictionary and the city is added to the list, else False

       """

       if country_name.lower() in self.citiesandcountries.keys():

           self.citiesandcountries[country_name.lower()].append(city_name.lower())

           return True

       else:

           return False

   def find_city(self, city_name):

       """

       This function is used to retrive a list of countries where the city with the given name is present

       :param city_name: Name of the city.

       :return: A list of all the countries where the city if present.

       """

       countries_with_city = []

       for country in self.citiesandcountries.keys():

           if city_name.lower() in self.citiesandcountries[country]:

               countries_with_city.append(country.title())

       return countries_with_city