← Back to challenges

Big Countries

PythonHardclassesobjectsmath

Instructions

A country can be said as being big if it is:

  • Big in terms of population.
  • Big in terms of area.

Add to the Country class so that it contains the attribute is_big. Set it to True if either criterea are met:

  • Population is greater than 250 million.
  • Area is larger than 3 million square km.

Also, create a method which compares a country's population density to another country object. Return a string in the following format:

{country} has a {smaller / larger} population density than {other_country}

Examples

australia = Country("Australia", 23545500, 7692024)
andorra = Country("Andorra", 76098, 468)

australia.is_big ➞ True
andorra.is_big ➞ False
andorra.compare_pd(australia) ➞ "Andorra has a larger population density than Australia"

Notes

  • Population density is calculated by dividing the population by the area.
  • Area is given in square km.
  • The input will be in the format (name_of_country, population, size_in_km2), where name_of_country is a string and the other two inputs are integers.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.