我根据我在 YouTube 上学到的关于类和对象的教程创建了一个简单的 MCQ,并将不同的 Python 程序导入其中。
以下是我创建的内容: 这是我创建的类。
class MCQ:
def __int__(self, prompt, answer):
self.prompt = prompt
self.answer = answer
Run Code Online (Sandbox Code Playgroud)
这是名为 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")
Run Code Online (Sandbox Code Playgroud)
这是错误。
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
Run Code Online (Sandbox Code Playgroud)
根据我所了解的,我了解到此错误是由于缺少不正确/未定义的参数而产生的。但是,我在类中分配了正确的参数,同时确保类的对象是正确的。为我对这个话题缺乏理解而道歉,我将非常感谢我能得到的所有帮助。
def __int__(self, prompt, answer):
Run Code Online (Sandbox Code Playgroud)
你在这里有一个错字;这应该使用__init__而不是__int__.
请注意,这__int__是一个有效的魔术方法,但它用于将您的类转换为 anint而不是构造您的类的实例。