Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
属性错误:';列表';对象没有属性';问题';关于Python_Python_Attributes_Attributeerror_Python Class - Fatal编程技术网

属性错误:';列表';对象没有属性';问题';关于Python

属性错误:';列表';对象没有属性';问题';关于Python,python,attributes,attributeerror,python-class,Python,Attributes,Attributeerror,Python Class,我遇到了以下问题AttributeError,但是我检查了语法和缩进,没有发现任何错误。有人能帮我找出哪里出了问题吗 class Question(): def __init__(self, question, answer): self.question = question self.answer = answer question_prompts = [ "What color are apples?\n(a) Red/Green

我遇到了以下问题AttributeError,但是我检查了语法和缩进,没有发现任何错误。有人能帮我找出哪里出了问题吗

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

question_prompts = [
    "What color are apples?\n(a) Red/Green\n(b) Purple\n(c) Orange\n",
    "What color are bananas?\n(a) Teal\n(b) Magenta\n(c) Yellow\n",
    "What color are strawberries?\n(a) Yellow\n(b) Red\n(c) Blue\n",
]

tasks = [
    Question(question_prompts[0], "a"),
    Question(question_prompts[1], "c"),
    Question(question_prompts[2], "b"),
]

def run_test(tasks):
    score = 0
    for items in tasks:
        answer = input(tasks.question)
        if answer == tasks.answer:
            score += 1
    print("You got " + str(score) + "/" + str(len(question_prompts)) + " correct")

run_test(tasks)
例外情况:

AttributeError: 'list' object has no attribute 'question'

您正在查看整个问题列表,并尝试为
.question
属性编制索引,以便它查看列表容器而不是单个对象,这将通过以下方法解决:-

answer = input(items.question)

As items是包含循环每次传递的问题对象的变量。您必须相应地更改答案检查,

任务
是一个列表<代码>项目是问题的一个实例。所以,
answer=input(items.question)