【问题标题】:Unresolved attribute reference '__' for class '__'类“__”的未解析属性引用“__”
【发布时间】:2022-01-15 14:36:08
【问题描述】:

我正在上一个初学者课程,当我到达这里时,讲师为课程制作了一个单独的文件,然后将其导入。我刚刚在顶部添加了这个类,因为我以前见过它工作。虽然它不像这样工作,但从另一个文件导入它是有效的。我做错了什么?

运行程序时的完整错误消息:

File "C:/Users/user/PycharmProjects/pythonProject1/app.py", line 54, in RunTest
    ans = input(Class.question)
AttributeError: type object 'Class' has no attribute 'question'
class Class:

    def __init__(self, question, answer):
        self.question = question
        self.answer = answer



QuestionPrompts = [
    "\n\n1. \na)\nb)\nc)\nd)\nYour Answer: ",
    "\n\n2. \na)\nb)\nc)\nd)\nYour Answer: ",
    "\n\n3. \na)\nb)\nc)\nd)\nYour Answer: ",
    "\n\n4. \na)\nb)\nc)\nd)\nYour Answer: ",
    "\n\n5. \na)\nb)\nc)\nd)\nYour Answer: ",
    "\n\n6. \na)\nb)\nc)\nd)\nYour Answer: ",
    "\n\n7. \na)\nb)\nc)\nd)\nYour Answer: ",
    "\n\n8. \na)\nb)\nc)\nd)\nYour Answer: ",
    "\n\n9. \na)\nb)\nc)\nd)\nYour Answer: ",
    "\n\n10. \na)\nb)\nc)\nd)\nYour Answer: "
]

questionArray = [
    Class(QuestionPrompts[0], "a"),
    Class(QuestionPrompts[1], "c"),
    Class(QuestionPrompts[2], "b"),
    Class(QuestionPrompts[3], "d"),
    Class(QuestionPrompts[4], "c"),
    Class(QuestionPrompts[5], "a"),
    Class(QuestionPrompts[6], "b"),
    Class(QuestionPrompts[7], "c"),
    Class(QuestionPrompts[8], "d"),
    Class(QuestionPrompts[9], "b")
]


def RunTest(questions):
    score = 0
    for question in questions:
        ans = input(Class.question)
        if ans == Class.answer:
            score += 1
        else:
            print("Aww man, you got this question wrong therefore lost :(\n"
                  "You got " + str(RunTest(questionArray)) + "/10 right tho!")
    return score


print("Your Score: " + str(RunTest(questionArray)) + "!") #added this part to check if I absolutely need to call the RunTest from outside

【问题讨论】:

  • 您需要发布完整的错误消息/堆栈跟踪。它会告诉你错误发生的确切位置。
  • class Class 没有名为 question 的属性; Class 的每个 instance 都会这样做。在您的循环中,questionClass 的一个实例。
  • 你肯定不想递归调用RunTest
  • @Ted-Klein-Bergman,我在帖子中添加了完整的错误
  • @chepner 对不起,我没明白,所以我要做的是添加一个名为 question 的属性,然后在 that 中引用循环?

标签: python class


【解决方案1】:

您将Class 对象列表传递给 RunTest 函数。

当您遍历此列表时,请参考当前对象的 .question.answer 属性。

def RunTest(questions):
    score = 0
    for question in questions:
        ans = input(question.question)
        if ans == question.answer:

【讨论】:

    【解决方案2】:

    这是因为您需要先创建该类的实例,

    例子:

    >>> class MyClass:
    ...    def __init__(self,question,answer) -> None:
    ...        self.question = question
    ...        self.answer = answer
    

    你需要先将类实例化为一个对象:

    >>> myobject = MyClass(question="how good is potato",answer="delicious")
    

    然后你可以调用self.question:

    >>> myobject.question
    "how good is potato"
    >>> myobject.answer
    "delicious"
    

    【讨论】:

    • 谢谢你的回答,我马上试试
    • 成功了吗??
    • 谢谢!我现在得到了这个
    • 好的,您也可以将此标记为答案吗?
    猜你喜欢
    • 2019-08-19
    • 2021-05-16
    • 2021-04-06
    • 2021-08-13
    • 2021-09-30
    • 2017-10-07
    • 2022-08-03
    • 2019-05-04
    • 2015-02-03
    相关资源
    最近更新 更多