Python Question Task 1 Course A course represents a specific course record as th
ID: 3829254 • Letter: P
Question
Python Question
Task 1 Course A course represents a specific course record as those appeared on your transcript. class course: Define the course class init elf, number credit, grade): ourse constructor. All three parameters must def be stored to instance variables of the same names (number, credit, and grade) str. Represents course number, like "cs112" or "MATH113 number int. Represents credit hours. You can assume it will always be a positive integer. credit ade str. Represents letter grade. Can only be or 'IP'. If the o F', provided grade is not one ofthese (for example, grade K"), then raise a CourseError with the message "bad grade ou can skip this exception-raising part until later elf): returns a human-centric string representation. If number def str CS112 A", then the returned string must be "CS112 credit 4, grade A edit- 4, and grade (note the four single spaces eq (self, other) we want to check that two courses are equal (our self and this other def course). We compare each instance variable like so (this is the definition!) return self. number other number and self.credit other credit and self. grade other grade def is passing (self) checks whether this course has passed or not according to grade. Return False if the grade is 'F' or 'IP';return True otherwise Task 2 Transcript This represents a group of course values as a list (named courses). We can then dig through this list for useful course information and calculations by calling the methods we're going to implement. class Transcript: Define the Transcript class (self): Transcript constructor. Create the only instance variable, a list named def init courses, and initialize it to an empty list. This means that we can only create an empty Transcript and then add items to it later on elf): returns a human-centric string representation. We'll choose a multi-line def str representation (slightly unusual) that contains "Transcript on the first line, and then each successive line is a tab, the str() representation of the next Course object in s and then a newline each time. This means that the last character of the string is guaranteed to be a new line (regardless of whether we have zero or many course values) eq (self, other): we want to check that two transcripts are equal (our self and this def other transcript). The only things that we need to compare are their lists of courses. Note: you can compare two lists 11 and 12 directly as 11 12 but this wi rely on your implementation of eq for course class since we are comparing two lists of courses. course) append the argument course to the end of self.courses. In def add cou self order to ensure every course number is unique, if our transcript already has a course of the same number (for example 'ENGH101 raise a courseError with the message "duplicate course umber 'ENGH101'". ou can skip this exception-raisin part until later def course by number(self, number Look through all stored course objects in self.courses Return the course object whose number matches the number argument. If no match, return None. You can assume that every course number is unique in a transcript. def course by grade self, ade) search through self.courses in order, return a list of Course object in this Transcript that is of this grade. If no match, return an empty list. def total passing credit (self): Look through all stored course objects in self.courses. Return total credit hours of all courses that have a passing gradeExplanation / Answer
class CourseError(Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
class Course:
def __init__(self, number, credit, grade):
self.number = number
self.credit = credit
gradeList = ['A', 'B', 'C', 'D', 'F', 'IP']
if grade in gradeList:
self.grade = grade
else:
raise CourseError("bad grade '" + str(grade) + "'")
def __str__(self):
return self.number + ": credit " + str(self.credit) + ", grade " + self.grade
def __eq__(self,other):
return self.number==other.number and self.credit==other.credit and self.grade==other.grade
def is_passing(self):
return not (self.grade == 'F' or self.grade == 'IP')
class Transcript:
def __init__(self):
self.courses = []
def __str__(self):
transcript = "Transcript: "
for course in self.courses:
transcript += str(course) + " "
return transcript
def __eq__(self,other):
return self.courses == other.courses
def add_course(self, course):
for crs in self.courses:
if course == crs:
raise CourseError("duplicate course number '" + course.number + "'")
self.courses.append(course)
def course_by_number(self, number):
for course in self.courses:
if course.number == number:
return course
return None
def course_by_grade(self, grade):
grade_list = []
for course in self.courses:
if course.grade == grade:
grade_list.append(course)
return grade_list
def total_passing_credit(self):
total_hours = 0
for course in self.courses:
if course.is_passing():
total_hours += course.credit
return total_hours
c1 = Course("CS112",4,"A")
print(c1.is_passing())
c2 = Course("ENGH101",3,"B")
print (c1==c2)
c3 = Course("MATH113",4,"IP")
t = Transcript()
print(str(t))
t.add_course(c1)
t.add_course(c2)
t.add_course(c3)
print(str(t))
t.add_course(Course("GAME101",3,"A"))
t.course_by_number("CS112")
print(t.course_by_number("CS112"))
t.course_by_number("CS114")
print(t.course_by_number("CS114"))
ans = t.course_by_grade("A")
print(len(ans))
c = Course("CS101",2,"Y") # this should raise a CourseError
# code link: https://pastebin.com/zaKDAAKs
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.