[英]TypeError: endturn() missing 1 required positional argument: 'self'
[英]TypeError: addQuiz() missing 1 required positional argument: 'score'
我收到此錯誤,我以為我已滿足所需的參數,但是我不確定自己做錯了什么以及該錯誤的確切含義。 我收到此錯誤:TypeError:addQuiz()缺少1個必需的位置參數:'score'
這是我為學生創建的課程:
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
編輯
這些方法不是類方法,是實例方法,創建實例並調用它們:
另外,更好地看一下,您還有另一種問題,我將評論:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.