2017-02-21 98 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

谢谢先生。非常感谢你帮助我。 –