Error in construction of 'class'

This is a sample Python project. Some thing wrong with this code. Nothing displays in the console. Only this message "Process finished with exit code 0 " Can any one help ?

class Student: #Instantiation of class
    studentcount = 0 #creating a class variable.

    def __init__(self, name, age,rollno, course):#constructing the first 'methode'(function)
        self.rollno = rollno
        self.name = name
        self.age = age
        self.course = course
        Student.studentcount =+ 1
    def displayCount(self):  # creating the second method.
        print('Total students :', Student.studentcount)

    def displayStudent(self):#creating the third method.
        print('Name = ',self.name)
        print('Age =',self.age)
        print('Rollno: ',self.rollno)
        print('Course :', self.course)

        st1 = Student ('Jayan',21,1001, 'MS')# First student object is created.
        st2 = Student('Beeran',27, 1002,'MCA')# Second student object is created.
        st3 = Student('Benny',25,1003,'MBA')# Third object created
        st4 = Student('Beena',23, 1004, 'MSW')# Fourth object created.
        st1.displayStudent()# displays the details of first student
        st2.displayStudent()# displays the details second student.
        st3.displayStudent()# displays the details third student.
        st4.displayStudent()# displays the details fourth student.
        print('Total Students :', Student.studentcount)

With the snippet, you’ve shared, all you’ve done is defined a class. There is nothing creating an instance of it or calling methods of the class.

In other words, this snippet is executing (basically) zero lines of code.

The issues are with the #displayStudent method. You probably didn’t intend to create instances of the Student class inside the #displayStudent method, but rather wanted to execute those lines immediately in the file.

Take them out from the #displayStudent method.


Just reply if you’re still having trouble. Just trying to hint at the problem/solution rather than providing code to paste.

1 Like

Hai @sethi , As advised by you I changed the code. I took out the class instance from the

[quote=“sethi, post:2, topic:451242”]
#displayStudent method
[/quote] Now it is working well. The revised code is given here for your reference. Thank you very much

class Student:
    studentcount = 0
    def __init__(self,rollno, name, age, course):#constructing the first 'methode'
        self.rollno = rollno
        self.name = name
        self.age = age
        self.course = course
        Student.studentcount = Student.studentcount +1
    def displayStudent(self):  # creating the second method.
        print('Roll No:%d, Name :%s, Age :%d, Course :%s' %(self.rollno,self.name,self.age,self.course) )

st1 = Student (1001, 'Jayan',21, 'MS')# First student object is created.
st2 = Student(1002, 'Biran',27, 'MCA')# Second student object is created.
st3 = Student(1003, 'Benny',25,'MBA')# Third object created
st4 = Student(1004, 'Beena',23,  'MSW')# Fourth object created.
st1.displayStudent()# displays the details of first student
st2.displayStudent()# displays the details second student.
st3.displayStudent()# displays the details third student.
st4.displayStudent()# displays the details fourth student.
print('No. of students :', Student.studentcount)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.