【问题标题】:TypeError: addQuiz() missing 1 required positional argument: 'score'类型错误:addQuiz() 缺少 1 个必需的位置参数:'score'
【发布时间】:2017-07-22 09:05:43
【问题描述】:

我收到了这个错误,我认为我满足了所需的参数,但我不确定我做错了什么以及这个错误究竟意味着什么。我收到此错误:TypeError: addQuiz() missing 1 required positional argument: '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

【问题讨论】:

    标签: python python-3.x


    【解决方案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
    
    ###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
    

    【讨论】:

    • 感谢您的修改和建议。我知道发生了什么,但我现在遇到的问题(我相信这是由于我想结束 while 循环时再次按 Enter)是:ValueError:无法将字符串转换为浮点数:
    • 我不知道该怎么做才能解决这个问题。我需要更改所有内容还是有简单的解决方案?
    • @HoonLee 好吧,如果你按下“enter”键,你会发送一个字符串,空字符串,“”,这不是一个浮点数,你可以通过添加一个 if 来修复它,或者一个例外,但我不知道如果用户写了一个错误的输入你会期待什么
    • @HoonLee 请参阅此问题以了解如何解决该问题:stackoverflow.com/questions/23294658/…
    • @SethMMorton 非常非常好的建议,我认为是这样的,但我不想将它添加到答案中,因为它回答了另一个问题......而且很难理解主要问题
    猜你喜欢
    • 2022-01-11
    • 1970-01-01
    • 2018-09-12
    • 2021-08-05
    • 2021-07-06
    • 2021-08-05
    • 2017-07-23
    • 2020-12-11
    相关资源
    最近更新 更多