我收到了这个错误,我认为我已经完成了所需的参数,但我不确定我做错了什么以及这个错误究竟意味着什么。我收到此错误:TypeError:addQuiz()缺少1个必需的位置参数:'得分'
这是我为学生创建的课程:
class Student:
def __init__(self, name):
self.name = name
self.score = 0
self.counter = 0
def getName(self):
return self.name
def addQuiz(self, score):
self.score += score
self.counter += 1
def get_total_score(self):
return self.score
def getAverageScore(self):
return self.score / self.counter
from Student import Student
x = input("Enter a student's name: ")
while True:
score = input("Enter in a quiz score (if done, press enter again): ")
quiz_score = Student.addQuiz(score)
if len(score) < 1:
print(Student.getName(x), Student.get_total_score(quiz_score))
break
答案 0 :(得分:2)
修改
这些方法不是类方法,是实例方法,创建实例,并调用它们:
另外,更好地看一下,你有另一种问题,我会评论:
class Student:
def __init__(self, name):
self.name = name
self.score = 0
self.counter = 0
def getName(self):
return self.name
def addQuiz(self, score):
self.score += score
self.counter += 1
def get_total_score(self):
return self.score
def getAverageScore(self):
return self.score / self.counter
###execution part (you can put it in a main... but as you want)
name = input("Enter a student's name: ") #create a variable name, and give it to the object you will create
student_you = Student(name) #here, the name as parameter now belongs to the object
score = float(input("Enter in a quiz score (if done, press enter again): ")) #you have to cast the input to a numerical type, such as float
while score > 1: #it is better to the heart of the people to read the code, to modify a "flag variable" to end a while loop, don't ask in a if and then use a break, please, just an advice
score = float(input("Enter in a quiz score (if done, press enter again): ")) #here again, cast to float the score
student_you.addQuiz(score) #modify your object with the setter method
print(student_you.getName(), student_you.get_total_score()) #finally when the execution is over, show to the world the result