2016-12-25 56 views
-2

位置參數:問題與我有以下的Python代碼塊有問題的Python

class Person: 
    def __init__(self, firstName, lastName, idNumber): 
     self.firstName = firstName 
     self.lastName = lastName 
     self.idNumber = idNumber 
    def printPerson(self): 
     print("Name:", self.lastName + ",", self.firstName) 
     print("ID:", self.idNumber) 

class Student(Person): 
     def _init_(self,firstName,lastName,idNum,scores): 
      self.scores = scores 
      Person.__init__(self, firstName, lastName, idNum) 

     def calculate(self): 
      s = 0 
      for n in self.scores: 
       s = s + n 
      avg = int(s/len(self.scores)) 
      if avg >= 90 and avg <= 100: 
       return 'O' 
      if avg >= 80 and avg <= 90: 
       return 'E' 
      if avg >= 70 and avg <= 80: 
       return 'A' 
      if avg >= 55 and avg <= 70: 
       return 'P' 
      if avg >= 40 and avg <= 55: 
       return 'D' 
      if avg < 40: 
       return 'T' 

    line = input().split() 
    firstName = line[0] 
    lastName = line[1] 
    idNum = line[2] 
    numScores = int(input()) # not needed for Python 
    scores = list(map(int, input().split())) 
    s = Student(firstName, lastName, idNum, scores) 
    s.printPerson() 
    print("Grade:", s.calculate()) 

我收到以下錯誤:

Traceback (most recent call last): 
File "solution.py", line 43, in <module> 
s = Student(firstName, lastName, idNum, scores) 
TypeError: __init__() takes 4 positional arguments but 5 were given 

任何人可以幫助我嗎?我認爲在初始化Students類的s對象時,我給出了正確數目的參數。

+0

你需要雙爲你的'Student'' __init__'方法加下劃線。我不完全確定這是怎麼導致這個錯誤,但這可能是你的問題。 – Carcigenicate

回答

2

的問題是,蟒蛇認爲你還沒有定義__init__因爲您不小心_init_(只有一個下劃線)

此代碼應該有希望做的伎倆寫:

class Student(Person): 
    def __init__(self,firstName,lastName,idNum,scores): # Changed _init_ to __init__ 
     self.scores = scores 
     Person.__init__(self, firstName, lastName, idNum) 

    def calculate(self): 
     s = 0 
     for n in self.scores: 
      s = s + n 
     avg = int(s/len(self.scores)) 
     if avg >= 90 and avg <= 100: 
      return 'O' 
     if avg >= 80 and avg <= 90: 
      return 'E' 
     if avg >= 70 and avg <= 80: 
      return 'A' 
     if avg >= 55 and avg <= 70: 
      return 'P' 
     if avg >= 40 and avg <= 55: 
      return 'D' 
     if avg < 40: 
      return 'T'