Python中位置参数的问题

2024-04-24 19:56:52 发布

您现在位置: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对象时给出了正确的参数数。你知道吗


Tags: andselfreturnifinitdeflinefirstname
1条回答
网友
1楼 · 发布于 2024-04-24 19:56:52

{{}因为您认为只有一个问题是用cd2定义的^}

这段代码应该很有希望做到:

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'

相关问题 更多 >