Python 属性错误:';列表';对象没有属性';字符串';
这是我的班级所在的文件Python 属性错误:';列表';对象没有属性';字符串';,python,Python,这是我的班级所在的文件 #8/23/2020 #Class file class Questions: def __init__(self, prompt, answer): self.prompt = prompt self.answer = answer 这是我的文本主体,运行时带有属性错误 #8/23/20 #Multiple Choice quiz import random from question_class import Questio
#8/23/2020
#Class file
class Questions:
def __init__(self, prompt, answer):
self.prompt = prompt
self.answer = answer
这是我的文本主体,运行时带有属性错误
#8/23/20
#Multiple Choice quiz
import random
from question_class import Questions
#These are my variables that I will use in the question_prompts array
number3 = random.randint(1,100)
number4 = random.randint(1,100)
number5 = random.randint(1,10)
number6 = random.randint(1,10)
number7 = random.randint(50,100)
number8 = random.randint(12,50)
answer3 = int(number3) + int(number4)
answer4 = int(number5) * int(number6)
answer5 = int(number7) - int(number8)
question_prompts = [ #This is my array for the question prompts
"Question 1:\nIn the directions NWSE what does E stand for?\n(a)Eagle\n(b)Eric\n(c)East\n(d)Enter\n",
"Question 2:\n(T/F) Tomatoes are fruits\n(T)\n(F)",
"Question 3:\nWhat is " + str(number3) + " + " + str(number4) + "?\n",
"Question 4:\nWhat is " + str(number5) + " x " + str(number6) + "?\n",
"Question 5:\nWhat is " + str(number7) + " - " + str(number8) + "?\n"
]
questions = [ #This is my array for my question class prompts and answers
Questions(question_prompts[0],"c"),
Questions(question_prompts[1],"f"),
Questions(question_prompts[2],str(answer3)),
Questions(question_prompts[3],str(answer4)),
Questions(question_prompts[4],str(answer5)),
]
def run_test(questions): #This is my function to run the quiz
score = 0
for question in questions:
answer = input(questions.answer)
if answer == questions.answer:
score += 1
print("You scored " + str(score) + "/" + len(questions.prompts))
run_test(questions)
我是新的编码,这个项目是一个选择题测验项目。我遵循Mike Dane的教程并试图使其动态化,但出现了错误。请使用初学者友好的单词作为开始,
回答==问题。回答是错误的,因为问题(带有s
)是问题列表,而不是问题。它应该是answer==question.answer
(不带s
)
其次,我不确定当问题只是给出答案而不是实际问你问题时,这个测验会有多困难:
answer = input(questions.answer)
我想您可能想传入问题。提示:-)作为一个开始,回答==问题。回答是错误的,因为问题(带有s
)是一个问题列表,而不是一个问题。它应该是answer==question.answer
(不带s
)
其次,我不确定当问题只是给出答案而不是实际问你问题时,这个测验会有多困难:
answer = input(questions.answer)
我想您可能想通过问题。请改为提示:-)在运行测试中有一些问题。应通过以下方式解决这些问题:
def run_test(questions): #This is my function to run the quiz
score = 0
for question in questions:
answer = input(question.prompt)
if answer == question.answer:
score += 1
print("You scored " + str(score) + "/" + str(len(questions)))
run_test(questions)
变化:
输入(问题.答案)
->输入(问题.提示)
[注意,此处有2处更改]
问题。回答
->问题。回答
len(问题.提示)
->str(len(问题))
[注意这里有两个更改]
run\u test
中存在一些问题。应通过以下方式解决这些问题:
def run_test(questions): #This is my function to run the quiz
score = 0
for question in questions:
answer = input(question.prompt)
if answer == question.answer:
score += 1
print("You scored " + str(score) + "/" + str(len(questions)))
run_test(questions)
变化:
输入(问题.答案)
->输入(问题.提示)
[注意,此处有2处更改]
问题。回答
->问题。回答
len(问题.提示)
->str(len(问题))
[注意这里有两个更改]