【发布时间】:2021-07-12 17:25:37
【问题描述】:
我遇到了以下问题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'
【问题讨论】:
-
tasks是一个列表。items是Question的一个实例。所以,answer = input(items.question).
标签: python attributes attributeerror python-class