Homework 12: Population Density
Objective: To write a program to select population density data from a text file
Write a program that reads this
text file of population data on all the countries of the world with over 1 million people (sorted by population)
(copied
from Wikipedia (data from 2018)). Your program should output a file in the same format as this one, but showing only the countries with population density higher than the amount entered by the user. For example:
[cpersiko@fog cs110a]$ python3 popDensity.py.txt
This program saves a file containing data on all the countries with population density above a specified number.
Please enter population density in people / square kilometer: 300
The file 'HighDensity.csv' has been saved with the list of all countries with population density above 300.0
[cpersiko@fog cs110a]$ cat HighDensity.csv
Country,Population,Area,Density
India,1354051854,3287240,364
Bangladesh,164994076,147570,1118
Japan,127387000,377873,337
South Korea,48456369,99538,487
Taiwan(China),23069345,35980,639
Sri Lanka,20238000,65610,308
Netherlands,17310000,41526,417
Belgium,10827519,30528,355
Haiti,10033000,27750,362
Rwanda,9998000,26338,380
Israel,7697600,20770,371
Hong Kong (China),7347000,1104,6349
Singapore,5612300,719.9,7796
Lebanon,4224000,10452,404
Palestinian Authority (disputed),4100000,6020,681
Puerto Rico (US),3982000,8875,449
Mauritius,1288000,2040,631
Bahrain,1234596,750,1646
[cpersiko@fog cs110a]$ python3 popDensity.py.txt
This program saves a file containing data on all the countries with population density above a specified number.
Please enter population density in people / square kilometer: 500
The file 'HighDensity.csv' has been saved with the list of all countries with population density above 500.0
[cpersiko@fog cs110a]$ cat HighDensity.csv
Country,Population,Area,Density
Bangladesh,164994076,147570,1118
Taiwan(China),23069345,35980,639
Hong Kong (China),7347000,1104,6349
Singapore,5612300,719.9,7796
Palestinian Authority (disputed),4100000,6020,681
Mauritius,1288000,2040,631
Bahrain,1234596,750,1646
[cpersiko@fog cs110a]$
Hints and Rules
- Notice that the first line of this particular file is a header line, with descriptive information about each piece of data. Make sure your program handles this line separately from the rest (as it has no numbers in it), and include it in the file you output.
- To split a comma-separated line of text (a string) into an array, use the string.split(",") function. The parameter indicates which character splits the string into elements.
- Your program should have at least 2 functions, including a main() function (no global variables or global code other than a call to main within an if-statement)
- Every function (except main) needs to have a comment in a triple-quoted string at the beginning, briefly explaining what it does.
- As with every assignment, you must write all the code yourself. Do not copy any code other people have written. If you use any Python features we haven't covered in class then the graders will search the internet for that code, to make sure you didn't copy it from a web site.
- Make sure to put a comment at the top of your program with your name, the name of this assignment and class (110A Homework 12), and a brief description of what the program does.
- Make sure to submit your homework using Canvas.