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

Python交互式学习工具统计查看功能异常:始终提示‘无可用问题’求助

Python交互式学习工具统计查看功能异常:始终提示‘无可用问题’求助

嗨,我帮你排查了代码里的几个关键问题,这应该就是导致你看不到已保存问题统计的原因,咱们一个个来修复:

1. load_questions函数中Freeform对象的创建逻辑错误

你在读取CSV里的Freeform类型问题时,不仅没有正确处理enabled的类型(CSV里存的是字符串,需要转成布尔值),还出现了重复添加的逻辑问题:Quiz类型的问题在分支里已经append了,但Freeform的append却写在分支外面,而且还会重复执行。

修复后的load_questions函数相关代码:

def load_questions(file_name="questions.csv"):
    '''
    Loads questions from a CSV file
    '''
    if not os.path.exists(file_name):
        print("No questions found. The file does not exist.")
        return []
    
    questions = []
    question_id = 1
    with open(file_name, encoding='utf-8') as file:
        reader = csv.DictReader(file)
        for row in reader:
            try:
                enabled = row.get('enabled', 'True').lower() == 'true'
                times_shown = int(row.get('times_shown', 0))
                correct_count = int(row.get('correct_count', 0))
                if row['type'] == 'quiz':
                    choices = row['choices'].split(', ')
                    answer = int(row['answer'])  # 把CSV里的字符串转成整数索引
                    question = Quiz(row['question'], choices, answer, enabled, times_shown, correct_count)
                    question.id = question_id
                    questions.append(question)
                    question_id += 1
                elif row['type'] == 'freeform':
                    # 正确传递转好的布尔值enabled,同时把append放在分支内
                    question = Freeform(row['question'], row['answer'], enabled)
                    question.id = question_id
                    questions.append(question)
                    question_id += 1
            except ValueError as e:
                print(Fore.RED + f"Error reading row: {row} - {e}" + Style.RESET_ALL)
                
    return questions

2. Quiz类的初始化参数不匹配

你在load_questions里创建Quiz对象时,传入了enabled, times_shown, correct_count三个额外参数,但Quiz类的__init__方法只接受4个参数(前三个必填,enabled可选),这会直接抛出参数不匹配的错误,导致Quiz类型的问题根本没被添加到列表里。

修复后的Quiz类:

class Quiz(Question):
    def __init__(self, question, choices, answer, enabled=True, times_shown=0, correct_count=0):
        # 把times_shown和correct_count传递给父类Question的初始化方法
        super().__init__(question, answer, enabled, times_shown, correct_count)
        self.choices = choices

3. view_statistics里的两个小问题

  • 你计算了correct_percentage,但打印的时候却输出了question.correct_count(正确次数),而不是百分比数值;
  • question.type这个属性不存在,需要通过判断实例类型来显示问题类型。

修复后的view_statistics函数相关代码:

def view_statistics(questions):
    '''
    Displays statistics for available questions
    '''
    if not questions:
        print(Fore.RED + "No questions available" + Style.RESET_ALL)
        return

    print("Question Statistics:")
    print("===============================================")
    for question in questions:
        correct_percentage = (question.correct_count / question.times_shown * 100 if question.times_shown > 0 else 0)
        # 通过实例类型判断问题类型
        question_type = "Quiz" if isinstance(question, Quiz) else "Freeform"
        # 显示正确的百分比,保留一位小数
        print(f"ID: {question.id} | Status: {'Enabled' if question.enabled else 'Disabled'}")
        print(f"Type: {question_type}")
        print(f"Question: {question.question}")
        print(f"Times Shown: {question.times_shown}")
        print(f"Correct Percentage: {correct_percentage:.1f}%")
        print("===============================================")

把这些修复点全部应用后,你再添加问题并保存,然后进入统计查看模式,应该就能正常看到所有已保存的问题和对应的统计数据啦!

备注:内容来源于stack exchange,提问作者Luthfi Pratomo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 17:19:35