# -*- coding: utf-8 -*- """ Created on Wed Oct 26 10:57:44 2022 @author: cpresser """ #import the data from another python file #import as a module and give it a name to use import WBexportData as exports #FUNCTIONS def average(numbers): sum = 0 for n in numbers: # add the current number into the sum sum += n #divide the sum by the length of the list return sum/len(numbers) #Fnd the country with the largest average #return the index of the country with the most average exports def maxAverageIndex(data): #keep track of the largest average maxValue = -1 #keep track of the index of the largest average maxIndex = -1 index = 0 for country in data: avg = average(country[2:]) #compute the average for this if avg > maxValue: maxValue = avg maxIndex = index index += 1 #return the index of the country with the largest value # and its average (as a tuple) return maxIndex, maxValue #MAIN PROGRAM #print(exports.fields) #test average function #print(average([5])) #test average with country data #slice the list to include only numbers #print(average(exports.countries[0][2:])) result = maxAverageIndex(exports.countries) indexOfMax = result[0] maxAvg = result[1] maxName = exports.countries[indexOfMax][0] maxCode = exports.countries[indexOfMax][1] print(f"""The country with the largest average exports is: {maxName} ({maxCode}) Average exports: $ {maxAvg:.2f}""")