Python 对不同的问题给予不同的分数
我如何让它对不同的问题给出不同的分数?第一个问题得30分,第二个和第三个问题得35分(总共100分) 谢谢Python 对不同的问题给予不同的分数,python,Python,我如何让它对不同的问题给出不同的分数?第一个问题得30分,第二个和第三个问题得35分(总共100分) 谢谢 from Question import Question question_prompts = ["What color are apples?\n(a) Red\n(b) Blue\n(c) Yellow\n\n", "What color are bananas?\n(a) Red\n(b) Blue\n(c) Yellow\n\n",
from Question import Question
question_prompts = ["What color are apples?\n(a) Red\n(b) Blue\n(c) Yellow\n\n",
"What color are bananas?\n(a) Red\n(b) Blue\n(c) Yellow\n\n",
"What color are strawberries?\n(a) Red\n(b) Blue\n(c) Yellow\n\n"]
questions = [Question(question_prompts[0], "a"),
Question(question_prompts[1], "c"),
Question(question_prompts[2], "b"),
]
def sayhi(name):
print("Hello " + name + "!\n")
sayhi("Alice")
def run(question):
score = 0
for getInput in question:
answer = input(getInput.prompt)
if answer == getInput.answer:
score += 2
print("You got " + str(score) + "/" + str(len(question *2)) + " correct!")
run(questions)
问题.py
class Question:
def __init__(self, prompt, answer):
self.prompt = prompt
self.answer = answer
首先,您需要将属性“score”添加到问题类中
class Question:
def __init__(self, prompt, answer, score):
self.prompt = prompt
self.answer = answer
self.score = score
然后,根据需要将每个问题的分数作为第三个参数传递
questions = [Question(question_prompts[0], "a", 30),
Question(question_prompts[1], "c", 30),
Question(question_prompts[2], "b", 35),
]
因此,您计算最终分数的逻辑更改为:
def run(question):
score = 0
for getInput in question:
answer = input(getInput.prompt)
if answer == getInput.answer:
score += getInput.score
print("You got " + str(score) + "/" + str(len(question * 2)) + " correct!")
如果不需要其他属性:
def run(question):
score = 0
for index, getInput in enumerate(question):
answer = input(getInput.prompt)
if (index == 0) and (answer == getInput.answer):
score += 30
elif answer == getInput.answer:
score += 35
print("You got " + str(score) + "/" + '100' + " correct!")
但总的来说,Chandra的方法更好如果没有其他限制,可以选择一种方法-在你的问题类中添加一个属性
分数
,并在计算分数时使用它。此外,您还需要单独计算/跟踪可能的总分
像下面这样的方法会起作用:
Question.py
class Question:
def __init__(self, prompt, answer, score):
self.prompt = prompt
self.answer = answer
self.score = score
def run(question):
score = 0
total_possible_score = 0
for getInput in question:
total_possible_score += getInput.score
answer = input(getInput.prompt)
if answer == getInput.answer:
score += getInput.score
print("You got " + str(score) + "/" + str(total_possible_score) + " correct!")
添加一个
score
属性?question\u prompt=[(“question”,value)]
可能就是您所需要的全部。