Practice Exercise - 5

1. Create a class named Course that has instance variables title, instructor, price, lectures, users(list type), ratings, avg_rating. Implement the methods __str__, new_user_enrolled, received_a_rating and show_details. From the above class, inherit two classes VideoCourse and PdfCourse. The class VideoCourse has instance variable length_video and PdfCourse has instance variable pages.

2. What is the output of this -

class Mother:
        def cook(self):
           print('Can cook pasta')

class Father:
        def cook(self):
             print('Can cook noodles')

class Daughter(Father, Mother):
          pass

class Son(Mother, Father):
         def cook(self):
             super().cook()
             print('Can cook butter chicken') 

d = Daughter()  
s = Son()

d.cook()
print()
s.cook()

3. What will be the output of this code -

class Person:
    def greet(self):
        print('I am a Person')

class Teacher(Person):
    def greet(self):
        Person.greet(self)    
        print('I am a Teacher')

class Student(Person):
    def greet(self):
        Person.greet(self)    
        print('I am a Student')

class TeachingAssistant(Student, Teacher):
     def greet(self):
         super().greet()
         print('I am a Teaching Assistant')
       
x = TeachingAssistant()
x.greet()

4. In the following inheritance hierarchy we have written code to add 'S' to id of Student, 'T' to id of Teacher and both 'T' and 'S' to id of Teaching Assistant. What will be the output of this code. If the code does not work as intended, what changes we need to make.

class Person:
    def __init__(self,id):
        self.id = id
        
class Teacher(Person):
    def __init__(self,id):
        Person.__init__(self,id)
        self.id += 'T'
    
class Student(Person):
    def __init__(self,id):
        Person.__init__(self,id)
        self.id += 'S'
   
class TeachingAssistant(Student, Teacher):
     def __init__(self,id):
        Student.__init__(self,id)
        Teacher.__init__(self,id)
       
x = TeachingAssistant('2675')
print(x.id)
y = Student('4567')
print(y.id)
z = Teacher('3421')
print(z.id)
p = Person('5749')
print(p.id)


Complete and Continue