What is wrong here with Choice 2?
Question: 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])
    


Answer: First of all:

    
    
    max(sort[i][1:2]) 
    

will return just the first of the pair of numbers, because the slice returns
elements `1` through `2-1`. You could change it to

    
    
    max(sort[i][1:3])
    

However, the fields that correspond to numbers in the .csv file are strings
(as they are returned from `csv.reader`). You should consider turning them to
ints when sorting. An easy way without using the `operator` thingy is using an
anonymous function that maps a string pair to a tuple of ints:

    
    
    sort = sorted(sort, key = lambda x: (int(x[1]), int(x[2])), reverse = True)
    # no need to put the rvalue in list()
    

which uses as key a tuple of integers that correspond to your number pair.
Also, if you are using `python 2.x`, you should either switch to `raw_input`
or use `input` and use `str(choice)`, as BigBang's answer points out.

To see why your key choice does not work when sorting according to the number
pair, keep in mind that the number pair is returned from csv.reader in a list
of strings (like ['Name', '12', '20']). It's easy to see that:

    
    
     >>> ('12', '20') > ('6', '20')
     False
    

This comparison fails to comply with the case of `12 > 6` because strings are
compared lexicographically. Here, '1' is smaller than '6'.

