2
#Variable containing the users class
Classes = ["classa","classb","classc"]
#Creates a variable so the textfile for ClassA can be opened.
OpenA = open ("ClassA.txt","a")
#Creates a variable so the textfile for ClassB can be opened.
OpenB = open ("ClassB.txt","a")
#Creates a variable so the textfile for ClassC can be opened.
OpenC = open ("ClassC.txt","a")
while True:
        Class = input("What class' results would you like to see?(ClassA, ClassB or ClassC)")
        #User friendly accepts the users answer if it is in higher or lower case
        Class = Class.lower()
        #Checks if the class provided by the user is from the three classes which have been provided
        if Class.lower() in Classes:
            #Leaves the while loop if all the conditions have been met
            break
        else:
            #Tells the user that the class which they had inputed was not correct
            print("Invalid, please choose a class from the list provided!")
if Class.lower() == ("classa"):
    print("Showing results for ClassA")
elif Class.lower() == ("classb"):
    print("Showing results for class B")
else:
    print("Showing results for class C")

The above code is the code which I have created so far to complete this task.

The main part in which I need help is creating a dictionary as the system should store the last three scores for each student. It will then also need to sort the results from the text files alphabetically, highest to lowest and by average. However, I do not know how to do this especially when dictionaries are involved. Would appreciate explanation with results to ensure that I am able to learn from this.

timgeb
  • 76,762
  • 20
  • 123
  • 145
  • You should use [`with`](http://preshing.com/20110920/the-python-with-statement-by-example/) statements rather than bare `open` calls like that. For multiple files like this, see [this question](https://stackoverflow.com/questions/3024925/python-create-a-with-block-on-several-context-managers). – Paul Mar 10 '16 at 16:21

1 Answers1

0

First off all, try not to start your variable names with a capital letter (e.g., OpenA should be openA). Second, don't use Class as a variable name as it's a reserved word for declaring new classes.

Regarding your question of sorting dictionaries: as stated by Devin Jeanpierre, it is not possible. You're better off using a list/sorted representation of the dict. To quote from the link:

It is not possible to sort a dict, only to get a representation of a dict that is sorted. Dicts are inherently orderless, but other types, such as lists and tuples, are not. So you need a sorted representation, which will be a list—probably a list of tuples.

But if you must use a dictionary, you can use a sorted representation of the dict. Here's the documentation on the sorted() function.

import operator
# dict initialization here, we'll call it dict1
result = sorted(dict1.items(), key=operator.itemgetter(1))

Will allow you to sort through the values of the dict and store it in a list of tuples.

Community
  • 1
  • 1
Chuck
  • 866
  • 6
  • 17