Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am stuck on United Nations The file UN.txt gives data about the 193 members of the United Nations. Each line of the file contains

I am stuck on

  1. 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).
  2. 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). Here is my code: Please help

Here is my code:

#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();

userInputB = input(" Enter a continent: ") continentData = [] densityData = [] print(" The five most densely populated U.N. member countries in that continent: ")

for i in readDict: tempData = readDict[i].continent

if tempData == userInputB: continentData.append(readDict[i]) densityData.append(readDict[i].popDensity(readDict[i].population,readDict[i].landArea))

descendOrderList = sorted(densityData, reverse = True) for j in range(5): indices = densityData.index(descendOrderList[j]) print(continentData[indices].countryname)

Here is the error I am getting:

Traceback (most recent call last): File "/home/poulako5/nation.py", line 75, in  from nation import * File "./nation.py", line 152, in  for i in readDict: NameError: name 'readDict' is not defined 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Domain Transfer Learning With 3q Data Processing

Authors: Ahmed Atif Hussain

1st Edition

B0CQS1NSHF, 979-8869061805

More Books

Students also viewed these Databases questions