[英]problem in attribute of an object in python
当我尝试使用属性时,会出现错误
AttributeError: type object 'Question' has no attribute 'prompt'
但这是我的代码
from hello import Question
questions=[
Question(question_sample[0],'a'),
Question(question_sample[1],'a'),
Question(question_sample[2],'a')
]
def run_test(test):
score=0
for question in test:
answer=input(Question.prompt)
if answer==Question.answer:
score+=1
print('you got'+str(score)+'/'+str(len(question_sample)))
run_test(questions)
我制作的模块是
class Question:
def __init__(self, prompt, answer):
self.prompt=prompt
self.answer=answer
请帮助为什么错误来了`
from project import Test
question_sample=["(1)What is facebook?\n(a)Social network\n(b)Toy\n(c)News website\n(d)Mobile\n\n",
'(2)What is Apple Inc.?\n(a)Social network\n(b)Toy\n(c)News website\n(d)Mobile Company\n\n',
'(3)What is Barbie?\n(a)Social network\n(b)Toy\n(c)News website\n(d)Mobile\n\n']
Questions=[
Test(question_sample[0], 'a'),
Test(question_sample[1], 'd'),
Test(question_sample[2], 'b')
]
def exam(x):
Score=0
for question in x:
answer=input(question.question)
if answer==question.answer:
Score+=1
print('you got')
我实际上很长一段时间都遇到了这个问题。 `
我猜你遇到了一个非常愚蠢的问题。 (它发生了)只需用Question
(小写字母 q)替换你的for
循环中的question
for question in test:
answer=input(question.prompt)
if answer==question.answer:
score+=1
此外,您需要在if answer==question.answer:
句子之后缩进。
希望这可以帮助。
使用 for 循环for question in test
您已秘密将所有Question(...,...)
重命名为question以便使用question访问它们
因此循环应该是
for question in test:
answer = question.prompt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.