1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| class Course: def __init__(self, name: str, grade: int, credits: int): self.__name = name self.__grade = grade self.__credits = credits def grade(self): return self.__grade def credits(self): return self.__credits def __str__(self): return f"{self.__name} ({self.__credits} cr) grade {self.__grade}" class StudyRecords: def __init__(self): self.courses = {} def add_completion(self, course_name, grade, op): if course_name in self.courses and self.courses[course_name].grade() > grade: return self.courses[course_name] = Course(course_name, grade, op) def get_completion(self, course_name): if not course_name in self.courses: return None return self.courses[course_name] def get_statistics(self): number_of_courses = len(self.courses) credits = 0 sum_of_grades = 0 grades = [0, 0, 0, 0, 0, 0, 0] for courses in self.courses.values(): credits += courses.credits() sum_of_grades += courses.grade() grades[courses.grade()] += 1 return { "number_of_courses": number_of_courses, "credits": credits, "average": sum_of_grades / number_of_courses, "grades": grades } class Application: def __init__(self): self.register = StudyRecords() def ohje(self): print("1 add course") print("2 get course data") print("3 statistics") print("0 exit") def new_completion(self): course_name = input("course: ") grade = int(input("grade: ")) op = int(input("credits: ")) self.register.add_completion(course_name, grade, op) def get_completion(self): course_name = input("course: ") courses = self.register.get_completion(course_name) if courses is None: print("no entry for this course") else: print(courses) def statistics(self): t = self.register.get_statistics() print(f"{t['number_of_courses']} completed courses, a total of {t['credits']} credits") print(f"mean {t['average']:.1f}") print("grade distribution") for i in range(5, 0, -1): grade_hits = t['grades'][i] row = "x"*grade_hits print(f"{i}: {row}") def execute(self): self.ohje() while True: print() command = input("command: ") if command == "0": break elif command=="1": self.new_completion() elif command=="2": self.get_completion() elif command=="3": self.statistics() Application().execute()
|