# grades.py # Walker M. White (wmw2) # September 30, 2015 """Grade Sheet Example for Dictionaries""" # Global variable store the grade sheet GRADES = {'js1':80,'js2':92,'wmw2':50,'aa1':95} def max_grade(grades): """Returns: maximum grade in the grade dictionary Parameter grades: the dictionary of grades Precondition: grades has netids as keys, ints as values""" maximum = 0 for v in grades.values(): if v > maximum: maximum = v return maximum def netids_above_cutoff1(grades,cutoff): """Returns: list of netids with grades above or equal cutoff Parameter grades: the dictionary of grades Precondition: grades has netids as keys, ints as values. Parameter cutoff: the grade cutoff Precondition: cutoff is an int.""" result = [] # start with an empty list for k in grades: if grades[k] >= cutoff: result.append(k) # Add k to the list result return result def netids_above_cutoff2(grades,cutoff): """Returns: list of netids with grades above or equal cutoff Parameter grades: the dictionary of grades Precondition: grades has netids as keys, ints as values. Parameter cutoff: the grade cutoff Precondition: cutoff is an int.""" result = [] # start with an empty list for k,v in grades.items(): if v >= cutoff: # NOTE: Alternate way to append to list result = result+[k] # Put k in a list and concatenate to end return result def give_extra_credit(grades,netids,bonus): """Gives bonus points to everyone in sequence netids This is a PROCEDURE. It modifies the contents of grades. It only modifies elements of grades with a key that appears in the sequence netids. Parameter grades: the dictionary of grades Precondition: grades has netids as keys, ints as values. Parameter netids: the netids to assign extra credit Precondition: netids is a sequence of strings whose elements are all keys in grades (though not all keys appear in netids). Parameter bonus: the amount of extra credit to assign Precondition: bonus is an int.""" for student in netids: if student in grades: # tests if student is a key in grades grades[student] = grades[student]+bonus