如何解决“ TypeError:“ example”()不带参数”错误?

时间:2018-10-18 15:27:51

标签: python

我根据我在YouTube上学到的有关类和对象的教程创建了一个简单的MCQ,并将不同的python程序导入到其中。

以下是我创建的内容:  这是我创建的类。

class MCQ:
    def __int__(self, prompt, answer):
        self.prompt = prompt
        self.answer = answer

这是第二个名为MCQ_Code的程序。

from MCQ_Code import MCQ
question_prompts = [
"What is the colour of a banana? \n(a) Red\n(b) Blue \n(c) Yellow\n(d) White\n\n",
"What is the colour of an apple? \n(a) Red\n(b) Blue \n(c) Yellow\n(d) White\n\n",
"What is the colour of blueberries? \n(a) Red\n(b) Blue \n(c) Yellow\n(d) White\n\n"
]

questions = [
MCQ(question_prompts[0], "c"),
MCQ(question_prompts[1], "a"),
MCQ(question_prompts[2], "b"),
]

def test(questions):
    score = 0
    for Qtion in questions:
        answer = input(Qtion.prompt)
        if answer == Qtion.answer:
            score += 1
    print("You got " + str(score) + "/" + str(len(questions)) + "correct")

这是错误。

Traceback (most recent call last):
File "D:/Python/PyCharm/Projects/MCQ.py", line 10, in <module>
MCQ(question_prompts[0], "c"),
TypeError: MCQ() takes no arguments

Process finished with exit code 1

根据我所学的知识,我知道此错误是由于缺少错误/未定义的参数而产生的。但是,在确保类的对象正确的同时,我在类中分配了正确的参数。抱歉,我对这个主题缺乏理解,对于获得的所有帮助我将不胜感激。

2 个答案:

答案 0 :(得分:3)

def __int__(self, prompt, answer):

您在这里有错字;应该使用__init__而不是__int__

请注意,__int__是有效的魔术方法,但是它用于将您的类转换为int,而不是构造您的类的实例。

答案 1 :(得分:2)

初始化函数称为__init__而不是__int__