Practice Activity : Comparing Population to Olympic Medal Counts
Introduction
We all love a good Olympic story. I mean, who didn't tear up when they watched the Jamaican bobsled team in Cool Runnings?
We all cheer for the good old U.S. of A. but we also like to cheer for the underdogs. But what exactly does it mean to be an underdog? Sure, a team that doesn't even really know about snow is an underdog. But other than that, a single medal won by a team from a tiny country just might be more impressive than 3 medals won by a much larger country.
In this activity you will look at the ratio of size of a country compared to the # of medals it won to try to figure out which countryreally performed better than we might expect.
Program Guide
For this assignment you should create the following functions in a file called olympics.py. You will make three functions. The first two are smaller, helper functions, that will be used by the third function
For this assignment you will be working with several potential data sets:
- 2012_Olympic_Medals.csv [From the summer Olympics in London, England] The larger file
- 2014_Olympic_Medals.csv [From the winter Olympics in Sochi, Russia] This is the smaller file
- 2018_Olympic_Medals.csv [ From the most recent winter Olylmpics in Korea]
Create a function called: totalPopulation()
This function should:
- Parameters:
- input file name - (you may use any of the files from above for testing)
- Actions:
- Opens the input file
- "Throws away" the header file
- Calculates the sum of the populations in the file
- Closes the file
- Returns:
- The total population of all of the countries who won medals at that Olympics
- Example Run
Create a function called: totalMedals()
This function should:
- Parameters:
- input file name - (you may use any of the files above for testing)
- Actions:
- Opens the input file
- "Throws away" the header file
- Calculates the sum of ALL of the medals won in the file
- Closes the file
- Returns:
- The total medal count for all of the countries who won medals at that Olympics
- Example Run
Create a function called: medalRatios()
This function should:
- Take Two Parameters:
- input file name - (you may use either of the files above for testing)
- output file name
- Returns:
- Nothing
- Actions:
- Uses the previous two functions to calculate the RATIO of how many medals were won for every billion (1,000,000,000) people.
- For example, in 2014 there were:
- 295 / 2,516,757,975 * 1000000000 =117.2 medals awarded for every billion people
- Opens the input file
- Opens the output file
- Pulls off the header and adds two more columns labeled "Medal Ratio" and "Classification"
- For each country provided in the data file you opened:
- extract population and total medals won just for that country
- calculate the ratio of medals to population per billion people as done above
- determines where this ratio lies compared to the overall average for the Olympics:
-
% of World Ratio Result <50% of World Ratio WELL Below Average 50% to 95% of World Ratio Below Average >95% but <105% Average 105% to 150% of World Ratio Above Average >150% of World Ratio WELL Above Average
-
- writes the original data PLUS the medal ratio (rounded to 2 decimal places) and the classification from the table above
- Closes both files
- Uses the previous two functions to calculate the RATIO of how many medals were won for every billion (1,000,000,000) people.
- Example Run
- Note, you do not need to include the print statement I did in my example. But you may.
- Example Output
Testing Your Code
- Go to the AutoLab Homepage for the course
- https://hypnos.cs.uni.edu/courses/CSED1320-F21/assessments
- Log in if necessary
- For each of the assignments listed above, find their submission link under the "Practice" heading
- Upload the corresponding python file for that assignment.
- When the "handin history" page comes up you will need to refresh the page until a score of either 0 or 1 is shown with your assignment.
- Click on the 0/1 score to get feedback.
- Read the information carefully if you received a score of 0 and figure out what you need to do to fix the problem.
- Ask for help from me or other students if you don't understand an error message.
- Resubmit until the program earns a 1.