如何针对不同的问题给它不同的分数?对第一个问题说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",
"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)
Question.py
class Question:
def __init__(self, prompt, answer):
self.prompt = prompt
self.answer = answer
答案 0 :(得分:0)
首先,您需要将'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!")
答案 1 :(得分:0)
如果您不想要其他属性:
change
但是一般而言,钱德拉的方法更好
答案 2 :(得分:0)
如果没有其他限制,可以选择以下一种方法-将score
属性添加到您的Question类,并在计算分数时使用它。另外,您需要单独计算/跟踪总可能得分。
类似以下的方法将起作用:
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!")