I have been working on this code (in python) to print a CSV file, sorted. The first choice works fine and sorts it Alphabetically. However the Choice 2 section is supposed to sort the csv file to the highest score. The text/csv file (
name, score, out of:
Ben,5,20
James,6,20
Adam,12,20
Will,20,20
code:
import operator
import csv
file = open("scores.txt", "r")
scores = csv.reader(file, delimiter=",")
sort = sorted(scores)
for i in range(0, len(sort)):
sort[i].append((max(sort[i][1:2])))
#Alphabetical Order
choice = input("Choice: ")
if choice == "1":
sort = list(sorted(sort,key = operator.itemgetter(0), reverse=False))
print("\nAlphabetical Order:")
print("===================")
for i in range(0, len(sort)):
print("Name: ", sort[i][0], "\tScore: ", sort[i][1])
#Highest score
#sort = sorted(scores)
elif choice == "2":
print("\nHigh Scores:")
print("============")
sort = list(sorted(sort,key = operator.itemgetter(1, 2),reverse=True))
for i in range(0, len(sort)):
print("Name:", sort[i][0], "\tScore:", sort[i][1], "Out of", sort[i][2])