Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using the program Thonny for the following Question: 1. United Nations The file

ID: 3718742 • Letter: U

Question

Using the program Thonny for the following Question:

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 country-name, con- tinent, population (in millions), and land area (in square miles). Some lines of the file are Canada, North America, 34.8,3855000 France, Europe, 66.3,211209 New Zealand, Australia/Oceania, 4.4,103738 Nigeria, Africa, 177.2.356669 Pakistan, Asia, 196.2,310403 Peru, South America, 30.1,496226 (a) Create a class named Nation with four instance variables to hold the data for a country and a method named pop Density 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.

Explanation / Answer

CODING:

class Nation:

    # constructor to initialize nation object with name, continent, area, population
    def __init__(self,name,continent,population,area):

        self.name=name
        self.continent=continent
        self.population=population
        self.area=area
      
    # method to get name
    def get_name(self):
        return self.name

    # method to get continent
    def get_continent(self):
        return self.continent

    # method to get population
    def get_population(self):
        return self.population

    # method to get area
    def get_area(self):
        return self.area

    # method to set name
    def set_name(self,name):
        self.name=name
      
    # method to set continent
    def set_continent(self,name):
        self.continent=name

    # method to set population
    def set_population(self,p):
        self.population=p

    # method to set area
  
    def set_area(self,a):
        self.area=a

PYTHON CODE(NationInfo.py):

from Nation import Nation
import pickle

# empty dictionary
d={}

# opening the file
f=open('UN.txt')


# looping through every line in the file
for line in f:

    # removing the spaces at the end
    line=line.strip()

    # splitting the line by comma
    data=line.split(',')

    # looping through every data in the line
    for item in data:

        # getting name of the country
        name=data[0].lower()

        # getting population
        pop=float(data[2]) * 1000000
          
        # getting area
        area=float(data[3])

        #formatting
        t1='{:,d}'.format(int(pop))
        t2='{:,.2f}'.format(area)

        # adding the country to dictionary
        if name not in d:

            ob=Nation(name,data[1],t1,t2)
            d[name]=ob            

# opening the file to pickel the dictionary
f2=open('nationsDict.dat','wb')

# dumping the dictionary
pickle.dump(d,f2)

# closing the file
f2.close()
f.close()


PYTHON CODE (main.py):

import pickle
import sys

d={}

# loading the already pickled data
try:
    # opening the file
    f=open('nationsDict.dat','rb')
  
    # loading the pickled data to dictionary
    d=pickle.load(f)
  
    # closing the file
    f.close()
     
except IOError:
    # if file not found, create a empty dictionary
    print('File not found')
    sys.exit()


# getting name from the user
country=input("Enter a country: ")

country=country.lower()

# checking for the country in the dictionary
for k,v in d.items():

    if k == country:
        print("Continent: ",v.get_continent())
        print("Population: ",v.get_population())
        print("Area: ",v.get_area(),' square miles')

CONTENTS OF UN.txt:(sample)

Afghanistan,Asia,31.8,251772
Albania,Europe,3.0,11100
Algeria,Africa,38.3,919595
Andorra,Europe,.085,181
Angola,Africa,19.1,481354

OUTPUT:
Enter a Country: peru

continent: south america

population:30,100,000

Area:49622600 Square miles