def list_years(data): years = set() tui.started("Listing years...") for records in data: years.add(records[9]) tui.display_years(years) tui.completed()
def tally_team_medals(data): tui.started("Tallying results for each team" ) # Calls function 'started()' with a message team_tally = {} # Creates empty dictionary 'team_tally' for record in data: # For each record (line) in the data team_name = record[ teams_from_data] # Variable 'team_name' to store the name data from the record[position] medal = record[ medals_from_data] # Variable 'medal' to store the name data from the record[position] if medal not in ( "Gold", "Silver", "Bronze"): # If no 'Gold', 'Silver' or 'Bronze' in medal continue # Continue to next line if team_name in team_tally: # If the team name is already in 'team_tally' team_tally[team_name][ medal] += 1 # Retrieve the nested medals dictionary for that team and increment the count else: team_tally[team_name] = { "Gold": 0, "Silver": 0, "Bronze": 0 } # Create the [medal] dictionary team_tally[team_name][medal] += 1 # And increment the count tui.display_medal_tally(team_tally) # Calls display function tui.completed() # Calls function 'completed()' from tui file
def list_years(data): tui.started("Listing years") years = set() for record in data: year = record[9] years.add(year) tui.display_years(years) tui.completed()
def tally_medals(data): tui.started("Tallying medals") medal_tally = {"Gold": 0, "Silver": 0, "Bronze": 0} for record in data: medal = record[14] if medal in ("Gold", "Silver", "Bronze"): medal_tally[medal] += 1 tui.display_medal_tally(medal_tally) tui.completed()
def list_years(data): tui.started("Listing years") # create set (will ignore duplicates) years = set() for record in data: year = record[9] years.add(year) tui.display_years(years) tui.completed()
def read_data(file_path): tui.started(file_path) data = [] with open(file_path) as file: csv_reader = csv.reader(file) headings = next(csv_reader) for values in csv_reader: data.append(values) tui.completed() return data
def read_data(file_path): tui.started(f"Reading data from {file_path}") data = [] with open(file_path) as csv_file: csv_reader = csv.reader(csv_file) next(csv_reader) for line in csv_reader: data.append(line) tui.completed() return data
def read_data(file_path): #code to read specified file path tui.started(f"Reading data from {file_path}") data = [] with open(file_path) as file: csv_reader = csv.reader(file) #ignore first line, i.e. headings next(csv_reader) for line in csv_reader: data.append(line) tui.completed() return data
def list_years(data): tui.started("Listing years...") # Calls function 'started()' years = set() # Create empty set called 'years' for record in data: year = record[ year_from_data] # Variable to store each record (year) in data years.add(year) # Add that data to the set 'years' tui.display_years( years ) # Calls function in tui file 'display_years' with 'years' at the parameter tui.completed() # Calls function in tui file 'completed()'
def read_data(file_path): # Function definition with parameter 'file_path' tui.started(f"Reading data from {file_path}") data = [] # Global variable 'data' = an empty list _headings = [] # Global UNUSED variable '_headings' = an empty list with open(file_path) as file: # Opens the file csv_reader = csv.reader(file) # Reads the file _headings = next(csv_reader) # Passes headings to variable '_headings' for line in csv_reader: # For the remaining lines in the file data.append(line) # Add each line to the list 'data' tui.completed() return data
def tally_medals(data): tui.started("Tallying medals") medals_tally = {"Gold: 0", "Silver: 0," "Bronze: 0"} # The function should iterate through each record in the list data and extract the medal. # If the medal is "Gold", "Silver" or "Bronze" then the relevant count in the dictionary should be incremented. for record in data: medal = record[col_medal] if medal in ("Gold", "Silver", "Bronze"): medals_tally[medal] += 1 # The dictionary should contain the total count of each medal once the code has finished iterating through the # list data. The appropriate functions from the module tui should be called to indicate the start of the # operation, display the tally of medals and indicate the end of the operation. tui.display_medal_tally(medals_tally) tui.completed()
def tally_medals(data): tui.started("Tallying medals...") medals = {"Gold": 0, "Silver": 0, "Bronze": 0} for records in data: if records[14] == "Gold": medals['Gold'] += 1 elif records[14] == "Silver": medals['Silver'] += 1 elif records[14] == "Bronze": medals['Bronze'] += 1 tui.display_medal_tally(medals) tui.completed()
def tally_team_medals(data): tui.started("Tallying medals for each team...") teams = {} for records in data: team = records[6] medal = records[14] if medal not in ('Gold','Silver','Bronze'): continue if team in teams: teams[team][medal] += 1 else: teams[team] = {"Gold": 0, "Silver": 0, "Bronze": 0} teams[team][medal] += 1 tui.display_team_medal_tally(teams) tui.completed()
def tally_team_medals(data): tui.started("Tallying medals for each team") medal_tally = {} for record in data: team = record[6] medal = record[14] if medal not in ("Gold", "Silver", "Bronze"): continue if team in medal_tally: medal_tally[team][medal] += 1 else: medal_tally[team] = {"Gold": 0, "Silver": 0, "Bronze": 0} medal_tally[team][medal] += 1 tui.display_team_medal_tally(medal_tally) tui.completed()
def tally_team_medals(data): tui.started("Tally Medals for each team") team_medals = {} for record in data: team = record[team_list] medal = record[medal_list] if medal not in ("Gold", "Silver", "Bronze"): continue if team in team_medals: team_medals[team][medal] += 1 else: team_medals[team] = {"Gold": 0, "Silver": 0, "Bronze": 0} team_medals[team][medal] += 1 tui.display_team_medal_tally(team_medals) tui.completed()
def tally_team_medals(data): tui.started("Tallying medals for each team.") # create nested dictionary team_tally = {} for record in data: team = record[6] medal = record[14] if medal not in ("Gold", "Silver", "Bronze"): continue # if team already in dictionary, increment count if team in team_tally: team_tally[team][medal] += 1 #if not already on dictionary, add team then increment count else: team_tally[team] = {"Gold": 0, "Silver": 0, "Bronze": 0} team_tally[team][medal] += 1 tui.display_team_medal_tally(team_tally) tui.completed()
def tally_medals(data): tui.started( "Tallying medals...") # Calls function 'started()' with a message tally = { "Gold": 0, "Silver": 0, "Bronze": 0 } # Creates a dictionary called 'tally' for record in data: # For each record (line) in the data medal = record[ medals_from_data] # Variable to store each medal (the record from [medals_from_data] column 14) if medal in ( "Gold", "Silver", "Bronze" ): # If the content is one of these keys... Gold, Silver, Bronze tally[medal] += 1 # Add one to the tally for that medal type tui.display_medal_tally(tally) # Calls the display function tui.completed() # Calls function 'completed()'