Question
Below is my code for part a and b: The issue I am having is trying to code for part c: I would greatly appreciate
Below is my code for part a and b: The issue I am having is trying to code for part c: I would greatly appreciate some assistance...with this portion of the question.
C. Write a program that requests the name of a continent as input, and then displays the names (in descending order) of the five most densely populated U.N. member countries in that continent. Use the pickled binary file nationsDict.dat and the file nation.py created in part (a).
- United Nations The file UN.txt gives data about the 193 members of the United Nations. Each line of the file contains four pieces of data about a countryname, continent, population (in millions), and land area (in square miles).
- Create a class named Nation with four instance variables to hold the data for a country and a method named popDensity that calculates the population density of the country. Write a program that uses the class to create a dictionary of 193 items, where each item of the dictionary has the form: name of a country: Nation Object for that country
Use the file UN.txt to create the dictionary, and save the dictionary in a pickled binary file named nationsDict.dat. Also, save the class Nation in a file named nation.py.
#class for Nation
class Nation:
#constructor for Nation
def __init__(self, tCountry="", tc="", tp=0, ta=0):
#assign the value for four parameters
self.country = tCountry;
self.continent = tc;
self.population = tp;
self.area = ta;
#setter method for country
def setCountry(self, tCountry):
self.country = tCountry;
#getter method for country
def getCountry(self):
return self.country;
#setter method for continent
def setContinent(self, tc):
self.continent = tc;
#getter method for continent
def getContinent(self):
return self.continent;
#setter method for population
def setPopulation(self, tp):
self.population = tp;
#getter method for population
def getPopulation(self):
return self.population;
#setter method for area
def setArea(self, ta):
self.area = ta;
#getter method for area
def getArea(self):
return self.area;
#method for find popDensity
def popDensity(self):
density = (float(self.population)/float(self.area))*10000
return '{0:2f}'.format(density)
from nation import *
from nation import Nation
#main function
def main():
#handle the nations object
nations = {};
try:
#for opening the file
fr = open("UN.txt", "r");
#Reading data from file
for line in fr:
#Stripping new line character and splitting on comma
lc = (line.strip()).split(",");
#creating a object for nation class
na = Nation(lc[0], lc[1],float(lc[2]),float(lc[3]));
# Storing in dictionary
nations[lc[0]] = na;
#close file
fr.close();
#reat the countryName name from user
countryName = input("Enter a country: ");
#check the the given country name is found in dictionary or not
if countryName not in nations.keys():
print(" Sorry! The given country name is not found ");
else:
#display the corresponding continent, Population and
#Area for given country
print("Continent: %s " %(nations[countryName].getContinent()));
print("Population: {:,d} ".format(int(nations[countryName].getPopulation() * 1000000)));
print("Area: {:,.2f} square miles ".format(nations[countryName].getArea()));
print("Density: {0} millions per sq.miles ".format(nations[countryName].popDensity()));
#for exception
except Exception as e:
print(e);
#calling main function
main();
- Write a program that requests the name of a U.N. Member country as input, and then displays information about the country as shown in fig. 7.20. Use the pickled binary file nationsDict.dat and the file nation.py created in part (a)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started