# return a dictionary in which the keys are bird types and the associated values are # the numbers of times that bird type appears in the input file. # def makeBirdDict(fileName): birdDict = {} inFile = open(fileName, 'r') # for each bird type read from file, if already in dictionary, # increment the associated count, otherwise add it to # dictionary with count of 1. for line in inFile: birdType = line.strip() if birdType in birdDict: birdDict[birdType] = birdDict[birdType] + 1 else: birdDict[birdType] = 1 inFile.close() return(birdDict) # Given a dictionary of the form returned by makeBirdDict, print # information about the most seen bird, i.e. the bird type with highest associated value # def mostSeenBird(birdDict): mostSeenBird = None mostSeenCount = None for key in birdDict: if (mostSeenCount == None) or (birdDict[key] > mostSeenCount): mostSeenBird = key mostSeenCount = birdDict[key] print("The most seen bird was '{}', sighted {:d} times.".format(mostSeenBird, mostSeenCount))