You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Python测验程序,为题目添加解释功能?

为Python测验程序添加题目解释的修改方法

1. 调整题目数据结构

原来的SAMPLE字典仅存储题目与选项列表,现在需要把每个题目的值改成**[选项列表, 解释文本]**的结构,让题目和解释关联起来:

SAMPLE = {
        'Who is the founder of Microsoft?': [
            ['Bill Gates', 'Bill Clinton', 'Jeff Bezos', 'Elon Musk'],
            '比尔·盖茨与保罗·艾伦在1975年共同创立了微软公司'
        ],
        'Who is the founder of Amazon?': [
            ['Jeff Bezos', 'Bill Gates', 'Elon Musk', 'Joe Biden'],
            '杰夫·贝佐斯在1994年创立亚马逊,最初以在线书店起家'
        ],
        'Who created Linux?': [
            ['Linus Torvalds', 'Elon Musk', 'Bill Gates', 'Mark Zuckerberg'],
            '林纳斯·托瓦兹在1991年发布了Linux内核的第一个版本'
        ] 
}

2. 修改循环中的数据提取逻辑

遍历题目时,需要把选项和解释从新的数据结构中拆分出来,替换原来直接获取选项的逻辑:

for num, (question, q_data) in enumerate(questionnaire, start=1):
    print ("{}. {}".format(num, question))
    currentQ += 1           
    alternatives = q_data[0]  # 提取选项列表
    explanation = q_data[1]   # 提取解释内容
    correct_answer = alternatives[0]
    # 后续原有逻辑保持不变

3. 作答完成后输出解释

不管用户回答正确与否,在判断结果输出后,添加解释的打印语句,格式符合你要的? 解释内容样式:

if answer == correct_answer:
    num_correct += 1
    print(f"Correct :  {keychoice}) {correct_answer}")
else:
    print(f"Wrong Answer  :  {answer_label}) {answer}") 
    print(f"Correct Answer:  {keychoice}) {correct_answer}")
# 新增:输出题目解释
print(f"\n? {explanation}")

修改后的完整代码

import random
from string import ascii_lowercase

def get_ext(file_type):
    SAMPLE = {
            'Who is the founder of Microsoft?': [
                ['Bill Gates', 'Bill Clinton', 'Jeff Bezos', 'Elon Musk'],
                '比尔·盖茨与保罗·艾伦在1975年共同创立了微软公司'
            ],
            'Who is the founder of Amazon?': [
                ['Jeff Bezos', 'Bill Gates', 'Elon Musk', 'Joe Biden'],
                '杰夫·贝佐斯在1994年创立亚马逊,最初以在线书店起家'
            ],
            'Who created Linux?': [
                ['Linus Torvalds', 'Elon Musk', 'Bill Gates', 'Mark Zuckerberg'],
                '林纳斯·托瓦兹在1991年发布了Linux内核的第一个版本'
            ] 
    }
    return locals()[file_type]

def startQuiz():
    try:
        questionnaire = get_ext("SAMPLE")
        tItems = len(questionnaire)
            
    except:
        print ("Something is wrong")
   
    questionPerStudent = min(3, len(questionnaire))
    questionnaire = random.sample(list(questionnaire.items()), k=questionPerStudent)
    num_correct = 0
    currentQ = 0

    for num, (question, q_data) in enumerate(questionnaire, start=1):
        print ("{}. {}".format(num, question))
        currentQ += 1           
        alternatives = q_data[0]
        explanation = q_data[1]
        correct_answer = alternatives[0]
        labeled_alternatives = dict(zip(ascii_lowercase, random.sample(alternatives, k=len(alternatives))))
        key_list  = list(labeled_alternatives.keys())
        val_list  = list(labeled_alternatives.values())
        position = val_list.index(str(correct_answer))
        keychoice = key_list[position]
        
        for label, alternative in labeled_alternatives.items():
            print(f"    {label}) {alternative}")

        while (answer_label := input("    Your answer is? ")) not in labeled_alternatives:
            print(f"    Please answer one of {', '.join(labeled_alternatives)}")
        answer = labeled_alternatives[answer_label]

        if answer == correct_answer:
            num_correct += 1
            print(f"Correct :  {keychoice}) {correct_answer}")
            
        else:
            print(f"Wrong Answer  :  {answer_label}) {answer}") 
            print(f"Correct Answer:  {keychoice}) {correct_answer}")
        
        # 输出题目解释
        print(f"\n? {explanation}\n")


startQuiz()

修改后的输出效果

1. Who is the founder of Microsoft?
    a) Jeff Bezos
    b) Bill Gates
    c) Elon Musk
    d) Bill Clinton
    Your answer is? a
Wrong Answer  :  a) Jeff Bezos
Correct Answer:  b) Bill Gates

? 比尔·盖茨与保罗·艾伦在1975年共同创立了微软公司

2. Who is the founder of Amazon?
    a) Joe Biden
    b) Elon Musk
    c) Bill Gates
    d) Jeff Bezos
    Your answer is? d
Correct :  d) Jeff Bezos

? 杰夫·贝佐斯在1994年创立亚马逊,最初以在线书店起家

内容的提问来源于stack exchange,提问作者KirYadz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 17:37:37