Define a class student in python with given specifications:

Instance variable   :   Name
                        Roll_Number
Methods     :   Getdata() – to input roll no. and name
            :   Printdata() – to diplay roll no. and name

Define another class marks which is derived from student class

Instance variable   :   Marks in 5 subjects
Methods     :   Inputdata() – To call Getdata and input the marks of 5 Subjects.
Outdata()-To call Printdata in order to display the marks in 5 subjects

Implement the above program in python


In [4]:
class student(object):
    '''Class to define a student object with the following attributes:
            -Name
            -Roll_Number
        Methods:
            -Getdata(): To input data
            -Printdata(): To display data'''
    def __init__(self):
        self.Name = ''
        self.Roll_Number = None
    def Getdata(self):
        self.Name = raw_input('Enter Name: ')
        self.Roll_Number = int(raw_input('Enter roll no: '))
    def Printdata(self):
        print 'Name: '+self.Name + '\n' + 'Roll No: ' + self.Roll_Number
class marks(student):
    '''Class to store marks of a student in 5 subjects'''
    def __init__(self):
        super(marks,self).__init__()
        self.marks = []
    def Inputdata(self):
        self.Getdata()
        for i in xrange(5):
            self.marks.append(int(raw_input('Enter marks for %d subject: '%i)))
    def Outdata(self):
        self.Printdata()
        for i in xrange(5):
            print 'Marks in %d subject: '%i + str(self.marks[i])

In [2]:
m = marks()
m.Inputdata()


Enter Name: 1
Enter roll no: 2
Enter marks for 0 subject3
Enter marks for 1 subject4
Enter marks for 2 subject5
Enter marks for 3 subject6
Enter marks for 4 subject7

In [ ]: