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

Python如何匹配不同列表存储的问题与对应答案

简易答题小程序实现方案

需求梳理

需要实现的核心功能如下:

  • 题目、答案分别存储在两个独立文本文件中,读取后转换为列表格式
  • 随机抽取题目展示给用户,抽过的题不重复出现
  • 用户手动输入答案后,自动和对应正确答案做校验
  • 答题后即时反馈对错,答错时展示正确答案
  • 代码逻辑简单直白,适合初学者理解

现有代码

当前编写的基础代码如下:

import random
def python():
   txt_file = open("python_mcqs.txt", "r")
   file_content = txt_file.read()
   #print("The file content are: ", file_content)

   content_list = file_content.split(",")
   txt_file.close()
   #print("The list is: ", content_list)
   question = []
   for x in content_list:
      question.append(x.replace("\n", ","))  
   #print(list(question))
   i= 1
   while i<=10:
      
      question_display = random.choice(question).rstrip()
      user_answer = input(question_display)
      if user_answer.lower() == "yes": # it is just a testing condition. yes will replace answers
         pass
      else:
         pass
   
      i = i + 1   
      
python()

注意:作为编程初学者,需要逻辑简单、易于理解的代码方案,降低学习成本。

实现思路

不用写复杂的去重判断逻辑,按最直白的步骤走即可:

  1. 调整文件存储格式:questions.txt里每一行存1道题,answers.txt里每一行存对应位置的正确答案(比如第1行题的答案就存在答案文件第1行),比用逗号拆分更不容易出错,毕竟题目本身可能带逗号
  2. 读取两个文件时直接按行拆成列表,天然保证题目和答案的位置一一对应
  3. 生成所有题目的序号列表,直接打乱序号顺序,之后按打乱的顺序取题,天然不会重复,不需要额外记录哪些题出过
  4. 循环答题时,取到对应序号的题目和答案,拿到用户输入后统一去掉前后空格、转成小写再比对,避免用户多打空格、大小写不一样导致误判
  5. 答完即时反馈对错,答错就展示正确答案,最后统计答对数量

完整可运行代码

import random

def quiz():
    # 1. 读取题目文件,按行拆分,每一行是一道题
    q_file = open("questions.txt", "r", encoding="utf-8")
    questions = []
    for line in q_file.readlines():
        # 去掉每行前后的换行、空格,跳过空行
        q = line.strip()
        if q:
            questions.append(q)
    q_file.close()

    # 2. 读取答案文件,顺序和题目一一对应
    a_file = open("answers.txt", "r", encoding="utf-8")
    answers = []
    for line in a_file.readlines():
        a = line.strip()
        if a:
            answers.append(a)
    a_file.close()

    # 提前检查题目和答案数量对不对得上,避免错位
    if len(questions) != len(answers):
        print("错误:题目数量和答案数量不一致,请检查两个文件!")
        return

    # 3. 生成题目序号列表,打乱顺序实现随机不重复
    index_list = list(range(len(questions)))
    random.shuffle(index_list)

    # 4. 开始答题,这里设置答10道题
    total_question = 10
    correct_num = 0
    for i in range(total_question):
        # 取打乱后的当前题号
        current_index = index_list[i]
        current_question = questions[current_index]
        correct_answer = answers[current_index]

        # 展示题目,收集用户答案
        print(f"\n第{i+1}题:{current_question}")
        user_answer = input("请输入你的答案:").strip()

        # 校验答案,忽略大小写和前后空格差异
        if user_answer.lower() == correct_answer.lower():
            print("✅ 回答正确!")
            correct_num += 1
        else:
            print(f"❌ 回答错误,正确答案是:{correct_answer}")

    # 答题结束展示结果
    print(f"\n答题结束!你总共答对了 {correct_num}/{total_question} 道题")

# 运行答题程序
quiz()

使用说明

  • 把所有题目全部放到questions.txt里,一行一道题
  • 把对应答案放到answers.txt里,一行一个答案,和题目顺序对齐
  • 两个文件和代码放在同一个文件夹下,直接运行代码即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.02 05:24:52