# grades.py # Walker M. White (wmw2) # September 30, 2012 """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 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 Precondition: grades has netids as keys, ints as values. 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 Precondition: grades has netids as keys, ints as values. 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. Precondition: grades has netids as keys, ints as values. netids is a sequence of strings whose elements are all keys in grades (though not all keys appear in netids). 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