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

如何解决Python测验题中选项重复及题答不匹配问题?

修复Python选择题测验程序的核心问题

问题梳理

  • 选择题选项存在重复(如出现多个相同选项)
  • 尝试移除已用选项后,出现问题与对应答案不匹配、正确答案未出现在选项中的异常情况

代码错误分析

  1. 动态修改遍历列表导致索引混乱:循环中直接移除questions_list的元素,原列表长度不断缩短,但循环变量i仍按初始长度递增,导致questions_list[i-1]取到的问题和answers_list[i-1]的答案无法正确配对。
  2. 答案列表处理逻辑错误:每次循环复制完整的answers_list,但因索引错误,误移除了非当前问题的正确答案,导致正确答案被排除出选项列表。
  3. 选项生成逻辑冗余:原代码手动维护current_answers_list并循环添加选项,容易因操作失误导致重复或遗漏。

修复方案

关键修改点

  • 将问题与答案配对成元组列表,避免索引错位问题
  • 生成错误选项时,直接从排除当前正确答案的列表中随机选取3个不重复的元素
  • 不再修改原问题/答案列表,避免遍历过程中的索引混乱

修复后的完整代码

import random

questions_list = [
    "What is a network on a small geographical area called?",
    "What is the topology where every device is connected to each other?",
    "What is 8 bits called?",
    "Which of these is a type of malware?",
    "Which of these is a protection against malware?",
    "What is a set of rules also known as?",
    "What carries out logic instructions in a CPU?",
    "What stores the instructions that are to be executed?",
    "What is the name of the process of transferring data between the CPU and RAM?",
    "Which of these is a type of optical storage device?"
]

answers_list = [
    "A LAN",
    "The Mesh Topology",
    "A Byte",
    "Worms",
    "A Firewall",
    "A Protocol",
    "The ALU",
    "The RAM",
    "The Fetch-Execute Cycle",
    "A DVD"
]

score = 0

def ask_question(questions, answers):
    global score
    # 将问题与答案配对,避免索引错位
    qa_pairs = list(zip(questions, answers))
    # 可选:打乱题目顺序
    # random.shuffle(qa_pairs)
    
    for q_num, (current_question, current_answer) in enumerate(qa_pairs, start=1):
        # 生成所有错误选项:排除当前正确答案
        wrong_answers = [ans for ans in answers if ans != current_answer]
        # 随机选3个不重复的错误选项
        selected_wrong = random.sample(wrong_answers, 3)
        # 组合选项并打乱顺序
        options_list = [current_answer] + selected_wrong
        random.shuffle(options_list)
        
        # 打印题目与选项
        print(f"""
        Question {q_num}:
        
          {current_question}
      
        Options:
          1.{options_list[0]}
          2.{options_list[1]}
          3.{options_list[2]}
          4.{options_list[3]}
          """)
        
        # 输入验证
        while True:
            try:
                answer = int(input("Enter your answer (option number): "))
                if 1 <= answer <=4:
                    break
                else:
                    print("Invalid answer - please enter a number between 1 and 4")
            except ValueError:
                print("Invalid input - please enter a valid number")
        
        # 检查答案
        chosen_answer = options_list[answer-1]
        if chosen_answer == current_answer:
            print("\nCorrect answer!")
            score +=1
        else:
            print("\nIncorrect answer :(")
            print(f"The correct answer is {current_answer}")
            if score >0:
                score -=1
        print(f"Your score is {score}\n")

# 调用函数
ask_question(questions_list, answers_list)

修复效果说明

  • 选项不再重复:使用random.sample直接选取不重复的错误选项
  • 问题与答案始终匹配:通过zip配对问题和答案,彻底避免索引错位
  • 正确答案必出现在选项中:选项列表初始就包含当前问题的正确答案,再添加错误选项后打乱

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 03:22:37