[Tutor] typerror

Dave Angel davea at ieee.org
Fri Dec 11 16:12:32 CET 2009


Roshan S wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">class 
> Student:
>     print"We have a new student "
>     def __init__(self,name='',credit=0,grade=0,quality=0):
>         self.name=name
>         self.credit=credit
>         self.grade=grade
>         self.quality=quality
>
>
>     def inputstudent(self):
>         self.name=raw_input("Enter student Name ")
>         self.credit=input("What da credit hours ")
>         self.grade=input("What da grade ")
>
>     def quality(self):
>         self.quality=self.credit*self.grade
>         print"Quality Points: ",self.quality
>
>     def average(self):
>         quality()
>         gpa=self.quality/self.credit
>         print"Grade point average: "+self.grade
>         if gpa == 4: print "Grade: A"
>         elif gpa == 3: print "Grade: B"
>         elif gpa == 2: print "Grade: C"
>         elif gpa == 1: print "Grade: D"
>
>     def outputstudent(self):
>         "Name: "+self.name
>
> #main
> #create new student
> stud1=Student()
>
> #run teh method
> stud1.inputstudent()
> stud1.outputstudent()
> stud1.quality()
> stud1.average()
>
>
> RUN
>
>
>>>>
> We have a new student
> Enter student Name r
> What da credit hours 3
> What da grade 5
> Traceback (most recent call last):
>   File "C:\Users\Roshan\Desktop\no3.py", line 38, in <module>
>     stud1.quality()
> TypeError: 'int' object is not callable
>>>>
>
>
> PYTHON version 2.6.3, windows 7
>
>
In your Student class, you're using attribute quality for two very 
different purposes.  You have a method by that name, and you have an 
instance variable by that name.  You need to call one of them something 
else, probably the instance variable.  Don't forget to change all the 
references to it, both in __init__() and in the method quality().


HTH

DaveA



More information about the Tutor mailing list