Due Thursday, November 8, at the start of class.
Write a Python script to read two data files about the world’s countries, store the data into useful data structures; then, based on user input, calculate basic world population statistics.
This is a slightly longer, more complex assignment than previous ones. If you are new to programming, you may copy the code that implements Part I (described below), to help you get started. Study it to see how the data structures work.
But, try to do both parts yourself. Be brave! The more you do on your own, the more you will learn!!!
For conceptual ease, I have broken down the overall script into two parts: reading the data files into a single, rich data structure, and then handling user input and the corresponding calculations. In both cases, you should write functions to organize the code and avoid repetition.
There are two separate data files containing country data. The first file is called input-05-country.txt, and looks like this:
ABW : Aruba : Aruba : Latin America & Caribbean ADO : Andorra : Principality of Andorra : Europe & Central Asia AFG : Afghanistan : Islamic State of Afghanistan : South Asia AGO : Angola : People's Republic of Angola : Sub-Saharan Africa ALB : Albania : Republic of Albania : Europe & Central Asia
That is, there are 4 data fields, separated by the string “ : ” (space - colon - space):
The second data file is called input-05-population.txt, and looks like this:
ABW : 1960 : 49205 ABW : 1961 : 50244 ABW : 1962 : 51258 ABW : 1963 : 52224 ABW : 1964 : 53117
There are 3 data fields, separated by the string “ : ” (space - colon - space):
Read both data files and store all of the data into a single data structure. If you look at the slides from today, you will see a code sample and diagram that (mostly) correspond to the homework.
Some suggestions:
split
function:
data_line.strip().split(' : ')
It is the opposite of the join
function: It takes a single string, and splits the string into
separate list elements by removing any instances of the argument. It returns a list of elements found. As
usual, you can look up this function (str.split) via interactive Python or online.
Now that you have data, it is possible to produce a simple report given some user input. To help in this part, you must write three functions (see below).
The objective is to ask the user for a country code and a year, then show the population for the corresponding country in that year, along with its percentage of the global population in the same year. Here is a sample dialog between the user and the script (as usual, user input is highlighted in yellow):
Code: USA Year: 1970 The population of United States in 1970 was 205052000, 5.594% of world population. Code: IND Year: 1982 The population of India in 1982 was 718425590, 15.696% of world population. Code: CHN Year: 2000 The population of China in 2000 was 1262645000, 20.826% of world population. Code:
When the user just presses Enter for the country code (i.e., enters the empty string), quit running the script.
For Part II, you must write three helper functions. Each one should be fairly short and easy, and together they make writing the main part of the code much easier. The functions are:
short_name(code)
None
.
population(code, year)
0
(the integer zero).
world_population(year)
0
(the integer zero). Hint: This function can use
the population()
function you just wrote!
Test your functions to make sure they work! Now, you can write the “main” code that gets input, calculates the answers, and prints the (one line) report.
Here are some specific tests to consider:
Start your script the right way! Here is a suggestion:
#!/usr/bin/env python """Homework for CS 368-4 (2012 Fall) Assigned on Day 05, 2012-11-05 Written by <Your Name> """
Do the work yourself, consulting reasonable reference materials as needed. Any resource that provides a complete solution or offers significant material assistance toward a solution not OK to use. Asking the instructor for help is OK, asking other students for help is not. All standard UW policies concerning student conduct (esp. UWS 14) and information technology apply to this course and assignment.
All homework assignments must be turned in by email! Attach your Python script to the email as a
text .py
file. See the email page for more details about formatting, etc.