2013-05-18 47 views
0

我是Python新手。我一直在關注關於python類的在線教程,但是我遇到了一個奇怪的錯誤。構造函數未能接受輸入參數

我無法弄清楚我做了什麼操作。

下面是我的代碼:

class StudentData: 
    "Contains information of all students" 
    studentNumber = 0; 
    def _init_(self,name,age,marks): 
     self.name = name; 
     self.age = age; 
     self.marsk = marks; 
    def displayStudentNUmber(self): 
     print 'Total Number of students = ',studentNumber; 
    def displayinfo(self): 
     print 'Name of the Student: ',name; 
     print 'Age of the Student: '.age; 
     print 'Marks of the Student: '.marks; 
student1 = StudentData('Ayesha',12,90) 
student2 = StudentData('Sarah',13,89) 
print "Student number in case of student 1",student1.displayStudentNumber(); 
print "Information of the Student",student1.dispalyinfo(); 
print "Student number in case of student 1",student2.displayStudentNumber(); 
print "Information of the Student",student2.dispalyinfo(); 

及以下爲錯誤

Traceback (most recent call last): File "main.py", line 14, in student1 = StudentData('Ali',12,90) TypeError: this constructor takes no arguments

任何人都可以解釋爲什麼我得到這個錯誤。

對不起一個跛腳的問題:(

回答

5

__init__()應該有兩邊兩個下劃線,所以在Python對待你的一樣只是一個普通的功能,在你的類

更改:

def _init_(self,name,age,marks): 

def __init__(self,name,age,marks): 
+0

非常感謝你:) – Ayse