Question
Python Code Issue: I am trying to perform step A for this question and keep getting this error. Can you review my code and tell
Python Code Issue: I am trying to perform step A for this question and keep getting this error. Can you review my code and tell me what I am doing wrong?
- 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 Nation: def __init__(self, country="", continent="", population=0.0, area=0): self._country = country self._continent = continent self._population = population self._area = area self._popDensity = (1000000 * float(self._population)) / int(self._area)
def setCountry(self, country): self._country = country
def setContinent(self, continent): self._continent = continent
def setPopulation(self, population): self._population = population
def setArea(self, area): self._area = area
def getCountry(self): return self._country
def getContinent(self): return self._continent
def getPopulation(self): return self._population
def getArea(self): return self._area
def popDensity(self): self._popDensity = (1000000 * self._population) / self._area return self._popDensity
with open('UN.txt','r') as f: out=f.read() out=out.split(' ') cntry_dict={} for inp in out: inp_cpy=inp.split(',') cntry_dict[inp_cpy[0]]= Nation(inp_cpy[0],inp_cpy[1],inp_cpy[2],inp_cpy[3])
import pickle
with open('nationsDict.dat', 'wb') as handle: pickle.dump(cntry_dict, handle, protocol=pickle.HIGHEST_PROTOCOL)
with open('nationsDict.dat', 'rb') as handle: cntry_dict_copy = pickle.load(handle)
out=input('Enter a Country:') print(""" Continent: %s Population: %s Area: %s square miles""" % (cntry_dict_copy[out]._continent,cntry_dict_copy[out]._population,cntry_dict_copy[out]._area))
Traceback (most recent call last): File "/home/poulako5/test.py", line 43, incntry_dict[inp_cpy[0]]= Nation(inp_cpy[0],inp_cpy[1],inp_cpy[2],inp_cpy[3]) File "/home/poulako5/test.py", line 7, in __init__ self._popDensity = (1000000 * float(self._population)) / int(self._area) ValueError: invalid literal for int() with base 10: '0.76' >>>
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