Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python 3 Coding Classes & Methods: Listed below are the three tasks I need help

ID: 3825504 • Letter: P

Question

Python 3 Coding Classes & Methods:

Listed below are the three tasks I need help with. Thank you!

A course represents a specific course record as those appeared on your transcript. class course: Define the course class. def_init_(self, number, credit, grade): Course constructor. All three parameters must be stored to instance variables of the same names (number, credit, and grade). number;; str. Represents course number, like "CS112" or "MATH113". credit;; int. Represents credit hours. You can assume it will always be a positive integer. grade;; str. Represents letter grade. Can only be 'A', 'B', 'C', 'D', 'F', or 'IP'. If the provided grade is not one of these (for example, grade == "K") then raise a CourseError with the message "bad grade 'K". (You can skip this exception-raising part until later). def_str_(self): returns a human-centric string representation. If number == "CS112", credit == 4, and grade == "A", then the returned string must be "CS112: credit 4, grade A" def_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.credit==other.credit 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.

Explanation / 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

# pastebin code link: https://pastebin.com/zaKDAAKs