I am creating a program that opens and reads a csv file and is sorted as follows:
- Alphabetical order with the highest score of each student.
- The highest score, from the highest to the lowest.
- Average score, from maximum to lowest.
The program should store the last 3 grades for each student. This is the part that I'm stuck in and need help with. When sorting a file in alphabetical order, the program should look at each student's last 3 recent grades and select the highest number. My code currently only sorts the file in alphabetical order. He reviews the last 3 ratings and selects the highest. I need help here.
My code already sorts the points from highest to lowest, but it displays all the grades received by each student, and does not print their highest score from their last 3 points.
Andrew 1
Andrew 2
Andrew 3
Andrew 4
Andrew 5
Finally, I need help calculating the average grade for each student. I guess how this should be done by adding Andrei the last 3 points, which are 5, 4 and 3 and are divided 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":
source
share