简体   繁体   中英

Unresolved attribute reference '__' for class '__'

I'm going through a beginner course and when I reached here the instructor made a separate file for the class and then imported it. I just added the class at the top because I've seen it work before. it doesn't work like this though but importing it from another file works. What am I doing wrong?

Full error message when running the program:

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

this is because you need to first create an instance of that class,

Example:

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

you need to first instanciate the class into an object:

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

then you can call the self.question:

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

You're passing a list of Class objects into the RunTest function.

When you iterate over this list, refer to the current object's .question and .answer attributes.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM