AttributeError: 'list' object has no attribute 'question' on Python
AttributeError: 'list' object has no attribute 'question' on Python
我遇到了以下问题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 是在循环的每次传递中包含 Question 对象的变量。您还必须相应地更改答案检查
我遇到了以下问题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 是在循环的每次传递中包含 Question 对象的变量。您还必须相应地更改答案检查