##Example 1 def totalPopulation(inputFile): fin= open(inputFile,"r") hearder = fin.readline() totalPop = 0 for row in fin: data = row.split(",") pop = int(data[1]) totalPop = totalPop + pop fin.close() return totalPop def totalMedals(inputFile): fin= open(inputFile,"r") hearder = fin.readline() total = 0 for row in fin: data = row.split(",") gold = int(data[2]) silver = int(data[3]) bronze = int(data[4]) total = total + (gold + silver + bronze) fin.close() return total def medalRatios(inputFile,outputFile): medals = totalMedals(inputFile) pop = totalPopulation(inputFile) worldRatio = medals/pop*1000000000 #print("worldRatio", worldRatio) fin = open(inputFile,"r") fout = open(outputFile,"w") header = fin.readline() fout.write(header.strip() + ",Medal Ratio,Classification\n") for row in fin: data = row.split(",") country = data[0] popC = int(data[1]) medalsC = int(data[2])+int(data[3])+int(data[4]) countryRatio=medalsC/popC*1000000000 fout.write(row[:-1]+","+ str(countryRatio)) fout.write(",") combo = countryRatio/worldRatio if (combo <= .5): fout.write("WELL Below Average") elif(combo <= .95): fout.write("Below Average") elif(combo < 1.05): fout.write("average") elif(combo < 1.5): fout.write("Above Average") else: fout.write("WELL Above Average") fout.write("\n") fin.close() fout.close() ######################### ##Example 2 def totalPopulation(filename_in): fin = open(filename_in,"r") header = fin.readline() total = 0 for population in fin: data = population.split(",") total = total + int(data[1]) fin.close return total def totalMedals(filename_in): fin = open(filename_in,"r") header = fin.readline() medals = 0 for medal in fin: data = medal.split(",") medals = medals + int(data[2]) + int(data[3]) + int(data[4]) fin.close return medals def medalRatios(filename_in,filename_out): fin = open(filename_in,"r") fout = open(filename_out,"w") header = fin.readline() fout.write(header[:-1]) fout.write(",Medal Ratio") fout.write(",Classification\n") totalRatio = totalMedals(filename_in)/totalPopulation(filename_in) totalRatio = (totalRatio)*1000000000 for row in fin: fout.write(row[:-1]) fout.write(",") data = row.split(",") ratio1 = int(data[2])+int(data[3])+int(data[4]) ratio2 = int(data[1]) ratio = (ratio1/ratio2)*1000000000 ratio = round(ratio,2) fout.write(str(ratio)) fout.write(",") if int(ratio)<(totalRatio*0.5): fout.write("WELL Below Average\n") elif int(ratio)<(totalRatio*0.95): fout.write("Below Average\n") elif int(ratio)<(totalRatio*1.05): fout.write("Average\n") elif int(ratio)<(totalRatio*1.50): fout.write("Above Average\n") else: fout.write("WELL Above Average\n") fout.close fin.close ######################### ##Example 3 def totalPopulation(file_in): fin=open(file_in,"r") header=fin.readline() population=0 for row in fin: data=row.split(",") population=int(population)+int(data[1]) fin.close() return(population) def totalMedals(file_in): fin=open(file_in,"r") header=fin.readline() medals=0 for row in fin: data=row.split(",") medals=int(medals)+int(data[2])+int(data[3])+int(data[4]) fin.close() return(medals) def medalRatios(file_in,file_out): medals=totalMedals(file_in) pop=totalPopulation(file_in) wr=medals/pop*1000000000 fin=open(file_in,"r") fout=open(file_out,"w") header=fin.readline() fout.write(header.strip()) fout.write(",Medal Ratio") fout.write(",Classification\n") med=0 pop=0 for row in fin: data=row.split(",") fout.write(row[:-1]) med=int(data[2])+int(data[3])+int(data[4]) pop=int(data[1]) mr=(med/pop)*1000000000 mr=round(mr,2) if (mr/wr)<.5: fout.write(",") fout.write(str(mr)) fout.write(",") fout.write("WELL Below Average") fout.write("\n") elif (mr/wr)>=.5 and (mr/wr)<.95: fout.write(",") fout.write(str(mr)) fout.write(",") fout.write("Below Average") fout.write("\n") elif (mr/wr)>=.95 and (mr/wr)<1.05: fout.write(",") fout.write(str(mr)) fout.write(",") fout.write("Average") fout.write("\n") elif (mr/wr)>=1.05 and (mr/wr)<1.5: fout.write(",") fout.write(str(mr)) fout.write(",") fout.write("Above Average") fout.write("\n") else: fout.write(",") fout.write(str(mr)) fout.write(",") fout.write("WELL Above Average") fout.write("\n") fin.close() fout.close() ######################### ##Example 4 def totalPopulation(ifile): fin= open(ifile,"r") header= fin.readline() totalPopulations=0 for row in fin: data=row.split(",") population=int(data[1]) totalPopulations=totalPopulations + population fin.close() return totalPopulations def totalMedals(ifile): fin= open(ifile,"r") header= fin.readline() totalMedal=0 for row in fin: data=row.split(",") goldmedals=int(data[2]) silvermedals=int(data[3]) bronzemedals=int(data[4]) totalMedal=totalMedal + (goldmedals+silvermedals+bronzemedals) fin.close() return totalMedal def medalRatios(ifile,ofile): medals= totalMedals(ifile) population= totalPopulation(ifile) worldRatio= medals/population*1000000000 print("The world ratio is ",round(worldRatio,1)," medals per billion people") fin = open(ifile, "r") fout = open(ofile, "w") header= fin.readline() fout.write(header.strip()+",Medal Ratio,Classification\n") for row in fin: data=row.split(",") #country=data[0] populationCountry=int(data[1]) medalsCountry=int(data[2])+int(data[3])+int(data[4]) countryRatio=medalsCountry/populationCountry*1000000000 #print(country, countryRatio) fout.write(row[:-1]) fout.write(","+str(round(countryRatio,2))) combined=countryRatio/worldRatio if combined <0.5: fout.write(",WELL Below Average") elif combined <=0.95: fout.write(",Below Average") elif combined <1.05: fout.write(",Average") elif combined <1.5: fout.write(",Above Average") else: fout.write(",WELL Above Average") fout.write("\n") fin.close() fout.close() return