Monday, 15 April 2013

sorting - I need to alter my code so that it shows highest to lowest -


I have done so in my code so that average is shown and students can be sorted alphabetically. However, I have to change my code so that the results can be stored from the highest to the lowest but I am struggling with it.

My text file looks like this: Name, 3,4,5 <2x, 2 names, 5,6,7
3 names, 3,4,5

< P> Where every name is different

This is my code so far:

  Students of class: def __init __ (self, name, number): self.name = Name self.marks = marks def avg_mark (self): return amount (self.marks) / len (self.marks) def __str__ (auto): return '{0}: {1}, {2}' Format (self.name, self.marks, self.avg_mark ()) def load_grades (filename): fd = open (file name, 'r') f = for partition f = {}: {= 1}} = [In the division for the difference (n) [1:]] try: F [key] = = Excluding KeyError: f [key] = students for points = [], value in f.items () for key Students.append (students (key, value [3:]) Return students def sort_by_name (stude (students): return student.name students = sorted (students, key = get_name) return students DEF sort_by_avg (students): Def get_avg (student): return student.avg_mark () students = sort (students) , Key = Get_avg) again tell the students again = true: class_num = input ('which class do you want to see 1,2 or 3?)) Print (input' A, B or C ') Print Print ('A') Print ('B) Average') Print ('C) lowest') Sort = Input ('How would you like to see it:') If class_num == "1" and Sort = = "A": Students = Load_grades ('class1.txt') students = sort_by_name (students) elif class_num == "1" and sort == "b": students = load_graduate ('class1.txt') students = students For In A sort_by_avg (students) students: print (student) again = input ('do you want to see or do not want to see another class yes or not?') = Then = (then == 'yes') print ('bye') < / Code> 

I want it so much that w chicken c is pressed that the number is reduced by the highest.

Thanks


No comments:

Post a Comment