Help on exercise 4 of 'Python Data Analysis Basics Practice Problems

Screen Link:

My Code:

def read_games(filename):
    #open and read the file
    from csv import reader
    opened_file = open(filename)
    read_file = reader(opened_file)
    file_data = list(read_file)
    return file_data[1:]

def yearly_dict(rows, year):
    year_dict = {}
    for game in rows:
        y = (game[2])
        name = game[0]
        global_sales = float(game[-1])
        if year == y:
            year_dict[name] = global_sales
    return max(year_dict, key = year_dict.get)

games = read_games('game_sales.csv')

year_list = []

for game in games:
     year = game[2]
     if year not in year_list:

 most_sold_per_year = {}
 for year in year_list:
    most_sold_per_year[year] = yearly_dict(games, year)
most_sold_1991 = most_sold_per_year['1991']

What I expected to happen:
I expected that the maximum global sales per year calculated would match that of the DQ answer

What actually happened:
For most of the years, my answers aligned with the DQ answer but in the years 2011, 2014, 2003, 2016 and 2012 I got different answers. I would be very grateful if someone could help me figure out the problem with my code.


Replace this line with the output/error

I realised that it was because there were some games which had multiple entries, and because a dictionary has to have unique keys, some of the game data was being overwritten