Ad

Storing Last 3 Scores And Deleting Older Scores And Calculating Average?

- 1 answer

I'm making a program which opens and reads a csv file and sorts in following ways:

  • Alphabetical order with each students highest score.
  • By the highest score, highest to lowest.
  • Average score, highest to lowest.

The program should store the last 3 scores for each student. This is the part where I'm stuck at and need help with. When sorting the file alphabetically the program needs to look at each students last 3 recent scores and select the highest number. Currently, my code only sorts the file in alphabetical order. It does looks at their recent 3 scores and selects the highest one. This is where I need help with.

My code already sorts the scores by highest to lowest however it prints out all the scores each student gained rather than printing their highest score out of their 3 recent score.

Andrew 1
Andrew 2
Andrew 3
Andrew 4
Andrew 5

Lastly I need help calculating the average score for each student. I'm guessing the way it should be done is by, adding Andrew's last 3 score which are 5, 4 and 3 and dividing by 3.

This is my code:

import csv, operator

selected_class = input("Pick a class file, (5, 6 or 7)? ")

print("1. Alphabetical order.")
print("2. Highest to lowest.")
print("3. Average score.")

selected_sorting = input("Pick an option 1, 2, or 3: ")

class_file = "Class " + selected_class + ".csv"
open_file = open(class_file)
csv_file = csv.reader(open_file)

if selected_sorting == "1":
    sorted_name = sorted(csv_file, key=operator.itemgetter(0))
    for i in sorted_name:
        print(i)

elif selected_sorting == "2":
    sorted_results = sorted(csv_file, key=lambda row: int(row[1]), reverse=True)
    for i in sorted_results:
        print(i)

elif selected_sorting == "3":
Ad

Answer

I will give some code for demonstration:

# -*- coding: utf-8 -*-
import csv
from collections import defaultdict
from statistics import mean

class_file = 'scores.csv'
open_file = open(class_file)
csv_file = csv.reader(open_file)


def main():
    # First, use student name to group by all scores, this will
    # generate structure like this:
    # {
    #     'Andrew': [1, 2, 3, 4, 5]),
    #     'Luck': [10, 20]),
    # }
    score_groups = defaultdict(list)
    for name, score in csv_file:
        score_groups[name].append(int(score))

    # Secondary, use the 3 latest socres only 
    l3_score_groups = [(key, value[-3:]) for key, value in score_groups.items()]

    print('1. Alphabetical order with each students highest score.')
    l3_highest_score_groups = [(key, max(values)) for key, values in l3_score_groups]
    for name, score in sorted(l3_highest_score_groups, key=lambda x: x[0]):
        print(name, score)

    print('2. By the highest score, highest to lowest.')
    l3_highest_score_groups = [(key, max(values)) for key, values in l3_score_groups]
    for name, score in sorted(l3_highest_score_groups, key=lambda x: x[1], reverse=True):
        print(name, score)

    print('3. Average score, highest to lowest.')
    l3_aver_score_groups = [(key, mean(values)) for key, values in l3_score_groups]
    for name, score in sorted(l3_aver_score_groups, key=lambda x: x[1], reverse=True):
        print(name, score)


if __name__ == '__main__':
    main()

Here are the technicals used above:

Hope it helps.

Ad
source: stackoverflow.com
Ad