Question

Use python programing.

We will look at the idea of how many olympic medals were won by a country compared to it's population. The 2014 population of the medal-earning countries that participated in the Sochi winter olymipics was 2,516,757,975 people. Further, 295 medals were won in the winter olympics in Sochi. This means that there were:

295 / 2,516,757,975 * 10,000,000 =1.172 medals awarded for every 10,000,000 people

For each country, we can use it's population and it's medal count to calculate whether it was above average, below average, WELL above average, or WELL below average based on:

Ratio Result
Less than 0.672 WELL Below Average
0.672 - 1.171 Below Average
1.172 Average
1.173-1.672 Above Average
Greater than 1.672 WELL Above Average

The Assignment

Download the following link to get the CSV file for the data of 2014 Winter Olympics in Sochi:

http://www.cs.uni.edu/~diesburg/courses/cs1510_sp17/homework/PA06/medalData.csv

This is a text file (although it is likely that your operating system will recognize it as openable by Excel and give it an Excel icon). Open this file with either Excel or a basic text editor. Notice that this file consists of a header line and then a sequence of data consisting of comma separate fields of country name, population, and medal count.

Write a program (called pa06.py) that:

opens this file for reading

reads and throws away the first line since it is the header line

for each country line:

splits it into six components (remember how to use the split method of a string)

adds the gold, silver, and bronze medal counts to get a total medal count

uses population and medal count to calculate it's average per 10 million people

prints to the screen a message with country, ratio, and an indication of whether this is above or below average

Once you get that working, modify the program to write to a file called results.txt rather than print to the screen.

HINT: Use the round function to round your computations to 3 decimal places. For example:

round (1.172142903, 3) 1.172

Finally, after you have finished writing to the results.txt file, print the country with the highest and lowest ratios.

NOTE: You do NOT need to use functions in this assignment.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PYTHON CODE

#importing the csv package to read the csv file
import csv

#variable to count the medal count
medal_count=0

#opening the second file to write the result
f=open('results.txt','w')

#dictionary to hold the country and their ratio
d={}

#opening the csv file
with open('medalData.csv') as csvfile:
    reader = csv.DictReader(csvfile)

    #for each counting the medal,ratio per population
    for row in reader:
        medal_count+=int(row['Gold'])
        medal_count+=int(row['Silver'])
        medal_count+=int(row['Bronze'])
        population=int(row['Population'])
        ratio=(medal_count/population)* 1000000
        msg=''
        if ratio < 0.672:
            msg="WELL Below Average"
        elif ratio>=0.672 and ratio <=1.171:
            msg="Below Average"
        elif ratio==1.172:
            msg="Average"
        elif ratio>= 1.173 and ratio <=1.672:
            msg="Above Average"
        elif ratio >1.672:
            msg="WELL Above Average"
        country=row['Country']
        country=country.strip()

        #writing to the file
        f.write(country+ ","+str(round(ratio,3))+ ","+msg+"\n")

        #entering the dictionary
        d[country]=round(ratio,3)

f.close()

#finding the maximum ratio country
maxi=0
maxi_country=''
for k,v in d.items():
    if v > maxi:
        maxi=v
        maxi_country=k
      
print('the country with maximum ratio '+maxi_country,end=' ')
print(maxi)

#finding the minimum ratio country

min_val=min(d.values())

mini=0
mini_country=''
for k,v in d.items():
    if v==min_val:
        mini=v
        mini_country=k
      

print('the Country with minimum ratio '+ mini_country,end=' ')
print(mini)


SCREENSHOT OF OUTPUT

the country with maximum ratio Latvia 144.624 the Country with minimum ratio China 0.161

Add a comment
Know the answer?
Add Answer to:
Use python programing. We will look at the idea of how many olympic medals were won...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT