Calling a class method raises a TypeError in Python

前端 未结 8 662
小鲜肉
小鲜肉 2020-12-04 16:02

I don\'t understand how classes are used. The following code gives me an error when I try to use the class.

class MyStuff:
    def average(a, b, c): # Get th         


        
8条回答
  •  时光说笑
    2020-12-04 16:59

    Try this:

    class mystuff:
        def average(_,a,b,c): #get the average of three numbers
                result=a+b+c
                result=result/3
                return result
    
    #now use the function average from the mystuff class
    print mystuff.average(9,18,27)
    

    or this:

    class mystuff:
        def average(self,a,b,c): #get the average of three numbers
                result=a+b+c
                result=result/3
                return result
    
    #now use the function average from the mystuff class
    print mystuff.average(9,18,27)
    

提交回复
热议问题