嗨,我是python的新手,我一直在获取TypeError:Question((不带参数
追踪(最近一次通话(:文件"/Users/adriankelsey/PycharmProjects/LearningPython/Building a Multiple Choice Quiz/app.py";,第10行,in问题(Question_prompts[0]、"a"(,TypeError:Question((不接受任何参数
这是问题.py
class Question:
def __int__(self, prompt, answer):
self.prompt = prompt
self.answer = answer
这在app.py 中
from Question import Question
question_prompts = [
"What color are apples?n(a) Red/greenn(b) Purplen(c) Orangenn",
"What color are bananas?n(a) Tealn(b) Magenta n(c) Yellownn",
"What color are strawberries?n(a) Yellown(b) Redn(c) Bluenn"
]
questions = [
Question(question_prompts[0], "a"),
Question(question_prompts[1], "c"),
Question(question_prompts[2], "b"),
]
def run_test(questions):
score = 0
for question in questions:
answer = input(question.prompt)
if answer == question.answer:
score += 1
print("You got " + str(score + "/" + str(len(questions) + " correct")))
run_test(questions)
它是def __init__(self, prompt, answer)
而不是
def __int__(self, prompt, answer)
您有拼写错误,请使用__init__
而不是__int__
。因为方法__init__
没有定义,所以它就像您的类Question
没有参数一样。