Project 1
- def open_file(filename):
Test whether the file assigned to variable filename is actually present. You will need to include the module os to do that, and use the the function path.isfile(). If the file is present, open it and return the file handle; if not a message should be printed, and the value None returned.
os.path.isfile(name):判断name这个文件是否存在,不存在返回false
- def get_units(unitfile):
Given the file handle for the opened units CSV file, this function returns a list of two item lists (or tuples), where the first item in each pair is the code for the unit, and the other is a floating point number representing the maximum mark achieved in that unit. For example, in the simple case described above, the function would return: [(“CITS1001”,95), (“CITS1401”,100), (“CITS1402”,93)] - def get_student_records(students_file, unit_count):
Given the file handle for the opened student marks CSV file, this function returns a list containing a list (or tuple) for each student. The first item is the name of the student, with the remaining items being floating point numbers or None. The second argument to the function, unit_count is used to check that the number of unit marks (or None values) reported for a particular student is the same as the number of units covered by this analysis. For example, if students_file is the file handle for the small example above, the function should return: [(“Needie Seagoon”,57,None,83), (“Eccles”,None,98,91), (“Bluebottle”,61,None,88)]. - def normalise(students_list, units_list):
Given the list of student records, and the list of units, return a new list of student records in which, for each of a student’s marks, the percentile mark is computed by dividing the actual mark by the maximum mark for the corresponding subject. That is, if there is a mark in the first marks column, it is divided by the first maximum mark, a mark in the second marks column is divided by the second maximum mark, and so on. In the small example discussed above, given the student marks list: [(“Needie Seagoon”,57,None,83), (“Eccles”,None,98,91), (“Bluebottle”,61,None,88)] and units list[(“CITS1001”,95), (“CITS1401”,100), (“CITS1402”,93)], this function should return: [[‘Needie Seagoon’, 0.6, None, 0.8924731182795699], [‘Eccles’, None, 0.98, 0.978494623655914], [‘Bluebottle’, 0.6421052631578947, None, 0.946236559139785]]. - def compute_mean_pc(students_pclist):
Given the new list of student records, with percentiles (floating point values less than or equal to 1.0) replacing the actual marks, this function computes the mean (average) percentile, ignoring the None values. In the small example, the returned list is: [(0.7462365591397849, ‘Needie Seagoon’), (0.979247311827957, ‘Eccles’), (0.7941709111488399, ‘Bluebottle’)]. Note that the order of items in each pair has been reversed. - def print_final_list(mean_pclist) :
Given the list of mean percentile value, student name pairs, print the list to standard output (screen by default), one per line, with the name appearing first. In other words, you should see:
Eccles 0.979
Bluebottle 0.794
Needie Seagoon 0.746 - def main():
main() pulls together all the other functions to implement the solution to this problem. main() prompts the user for the name of the unit and maximum marks file, and the name of the student marks file, and then computes and prints out the ranked list, as described above. While all the functions are important, it is particularly important that you define main() because your program will be tested via calls to main(), so if you fail to implement main(), it will be impossible to test your program.