Roxanne A.
asked 01/05/23Can someone help explain this to me in a simple way? And provide a sample? Thanks!
Part 1
First create a single .py file that contains both the Country class definition and then a main program that uses instances of that class, similar to the way classSample.py works. The Country class should have the following properties:
- name of the country (string data)
- population of the country (integer data)
- name of the country's capital (string data)
- population of the capital (integer data)
The Country class should have:
- an initializing constructor. You can decide which properties are initialized.
- a toString() method that prints the data for the country. You can decide on the format of the output.
- at least two other methods, one of which should include input. For example, you could have a method that allows a user to update the population of a country, or any other property of the Country.
The software that uses instances of the class should create at least two different instances of a Country, and should include Python instructions testing all of the methods in your class and demonstrating that they work properly.
Part 2
You should decouple the software you created in Part 1 by putting the Class definition in its own file, with the software that uses instances of the class in a separate file, similar to the way myprogram.py uses bookClass.py. You do not need to create new software from scratch for this part of the assignment -- you can use the same software from Part 1, but it should be packaged in two different files.
Your software should be properly documented.
You should submit all three .py files -- the first file with both class definition and the software using instances of the class in the same file, and the two files with the same software using the class as a decoupled class. It is okay to upload all three files independently -- they do not need to be zipped together.
2 Answers By Expert Tutors

Hayward B. answered 01/05/23
python developer, software enginneer
Here is a simple example of a Country class in Python:
Copy code
class Country:
def __init__(self, name, population, capital, capital_population):
self.name = name
self.population = population
self.capital = capital
self.capital_population = capital_population
def __str__(self):
return f"Country: {self.name}\nPopulation: {self.population}\nCapital: {self.capital}\nCapital Population: {self.capital_population}"
def update_population(self, new_population):
self.population = new_population
def update_capital(self, new_capital):
self.capital = new_capital
# Create two instances of the Country class
country1 = Country("USA", 330 million, "Washington D.C.", 700 thousand)
country2 = Country("France", 67 million, "Paris", 2.2 million)
# Print out the data for each country
print(country1)
print(country2)
# Update the population of country1
country1.update_population(350 million)
# Update the capital of country2
country2.update_capital("Marseille")
# Print out the updated data for each country
print(country1)
print(country2)
For Part 2, you would put the Country class definition in its own file, called "country.py". Then, in another file called "main.py", you would import the Country class and create instances of it, like in the example above. Here is what the main.py file would look like:
Copy code
from country import Country
# Create two instances of the Country class
country1 = Country("USA", 330 million, "Washington D.C.", 700 thousand)
country2 = Country("France", 67 million, "Paris", 2.2 million)
# Print out the data for each country
print(country1)
print(country2)
# Update the population of country1
country1.update_population(350 million)
# Update the capital of country2
country2.update_capital("Marseille")
# Print out the updated data for each country
print(country1)
print(country2)
Make sure to properly document your code by adding comments explaining what each part of the code does.
Roxanne A.
the main.py file gives me an error01/13/23
William M. answered 01/05/23
STEM Tutor (Ph.D, Northwestern) | Algebra–Calc, Bio, Chem, Physics, CS
'''This is a PARTIAL answer to the question,
which looks like a homework assignment for a Python class
As such: the student should complete the assignment themselves,
with assistance from the tutor, not the complete solution
To run this code, you will need the following (uncommented) code in main.py
from country import Country
def main():
Country.run_tests()
if __name__ == '__main__':
main()
'''
# the following code should be in country.py
class Country:
'''class for representing minimal information of a country
its name, population as of 2021, and
its capital, and capital's population as of 2021
'''
def __init__(self, country: str, country_pop: int, capital: str, capital_pop: int):
self.country, self.country_pop = country, country_pop
self.capital, self.capital_pop = capital, capital_pop
def __str__(self): # uses >16 for right-justified, 16 width of string and
# uses , to separate large numbers (English convention only)
return f'{self.country:>16} ({self.country_pop:,}), capital: {self.capital:8} ({self.capital_pop:,}) pop. figures as of 2021'
def toString(self): return self.__str__() # Note: toString() is a Java convention
def update_capital_pop(self, population):
delta_pop = population - self.capital_pop
self.capital_pop = population
self.country_pop += delta_pop
def update_country_pop(self, population):
self.country_pop = population
def update_country_pop_by_percentage(self, percentage):
fraction = percentage / 100.0
self.country_pop = self.country_pop * (1.0 + fraction)
def update_country(self, country):
self.country = country
def update_capital(self, capital):
self.capital = capital
@staticmethod
def run_tests():
countryA = Country(country='France', country_pop=83_130_000,
capital='Paris', capital_pop=2_161_000)
countryB = Country(country='Germany', country_pop=67_500_000,
capital='Berlin', capital_pop=3_645_000)
# print(f'countryA is: {countryA}') # this is the correct Python convention
# print(f'countryB is: {countryB}')
# test creation of countries
print(f'\n')
print(f'countryA is: {countryA.toString()}') # Note: this is a JAVA convention for strings
print(f'countryB is: {countryB.toString()}')
# test change of name of countries
countryA.update_country('Nouvelle France')
countryB.update_country('Neu Germany')
print('\nafter the name changes, the countries are...')
print(f'countryA is: {countryA.toString()}')
print(f'countryB is: {countryB.toString()}')
# test change of population of countries
delta_percentage = 10.0
countryA.update_country_pop_by_percentage(-10.0)
countryB.update_country_pop_by_percentage(+30.0)
print('\nafter percentage changes in population, the countries are...')
print(f'countryA is: {countryA.toString()}')
print(f'countryB is: {countryB.toString()}')
# test increase in population of country's capitals
countryA.update_capital_pop(countryA.capital_pop + 1_000_000)
countryB.update_capital_pop(countryB.capital_pop + 2_000_000)
print('\nafter changes in countries\' populations, the countries are...')
print(f'countryA is: {countryA.toString()}')
print(f'countryB is: {countryB.toString()}')
print(f'\n')
#=================================================================================
# OUTPUT must be run from main.py (see code at top of file)
#=================================================================================# countryA is: France (83,130,000), capital: Paris (2,161,000) pop. figures as of 2021
# countryB is: Germany (67,500,000), capital: Berlin (3,645,000) pop. figures as of 2021
# after the name changes, the countries are...
# countryA is: Nouvelle France (83,130,000), capital: Paris (2,161,000) pop. figures as of 2021
# countryB is: Neu Germany (67,500,000), capital: Berlin (3,645,000) pop. figures as of 2021
# after percentage changes in population, the countries are...
# countryA is: Nouvelle France (74,817,000.0), capital: Paris (2,161,000) pop. figures as of 2021
# countryB is: Neu Germany (87,750,000.0), capital: Berlin (3,645,000) pop. figures as of 2021
# after changes in countries' populations, the countries are...
# countryA is: Nouvelle France (75,817,000.0), capital: Paris (3,161,000) pop. figures as of 2021
# countryB is: Neu Germany (89,750,000.0), capital: Berlin (5,645,000) pop. figures as of 2021
#=================================================================================
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Judy R.
Hello I am 22 years experienced Online Tutor and Assignment Helper for Computer Science and Math. I have been teaching IT Professionals, students from different grades, and Graduate and Post Graduate students for more than 22 years. I am ready to help you with your learning requirements, assignments, tests, and projects. For further discussion about the assignment or project help you need, please add me on skype and my skype id is nettuitions Thanks01/22/23