2017-02-21 79 views
-3
class EceA: 
    def __init__(s,name,age): 
     s.name = name 
     s.age = age 
    def disp(): 
     return("the student name is" + s.name +"and the age is"+str(s.age)) 

reg1=("sam",21) 


Traceback (most recent call last): 
    File "<pyshell#27>", line 1, in <module> 
    reg1.disp 
AttributeError: 'tuple' object has no attribute 'disp' 
+2

您錯過了您的班級名稱,例如: 'reg1 = EceA('sam',21)',你缺少'disp()'的'instance'變量。注意調用'instance'變量'self'是很傳統的(你在'__init __()'中使用了''''這些都是基本的錯誤,我會閱讀更多初學者的教學材料 – AChampion

回答

1

你需要讓類的一個對象與參數:

reg1 = EceA("sam",21) 
reg1.disp() 

還需要self放慢參數傳遞給disp()功能,如:

def disp(self): 
     return("the student name is" + self.name +"and the age is"+str(self.age)) 

此外,在init您應該通過'self'而不是's'作爲第一個參數

請閱讀https://docs.python.org/3/tutorial/classes.html

+1

'''''''使用'str.format()'可能是一種更好的方法:'返回'學生姓名是{0.name},年齡是{0.age}「。format(self)' – AChampion

+0

謝謝先生。非常感謝你幫助我。 –

相關問題