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

如何扩展基于集合的知识测验程序以支持多组题目?

多题目组知识测验程序改造方案

需求说明

现有一款基于集合的知识测验程序,仅支持行星相关题目,希望扩展为支持欧盟成员国、DAX成分公司等多组题目。

原代码

planeten = {'Merkur', 'Venus', 'Erde', 'Mars'}
planetenx = {''}
richtige = 0
versuche = 0

print('List all the planets in our solar system!')
while planeten != set():
    eingabe = input('Planet: ')
    if eingabe in planeten and eingabe not in planetenx:
        planeten = planeten - {eingabe}
        planetenx.add(eingabe)
        print('Richtig!')
        print(planetenx)
        richtige += 1
        versuche += 1

    elif eingabe not in planeten and eingabe in planetenx:
        print('Sorry!', eingabe, 'we already had')
        versuche += 1

    else:
        print('Sorry!', eingabe, 'is not a planet')
        versuche += 1

print('Congratulations. You have listed all the planets.')
print(richtige, 'richtige Antworten, in ', versuche, 'Versuchen')

改造思路

  1. 将测验核心逻辑封装为通用函数,脱离具体题目组的绑定
  2. 把所有题目组统一配置化,方便新增和管理
  3. 添加题目选择交互,让用户自主选择要参与的测验

改造后代码

def run_quiz(quiz_name, correct_items, prompt_text):
    """通用测验执行函数,适配任意题目组"""
    answered_items = set()
    correct_count = 0
    total_attempts = 0
    remaining_items = correct_items.copy()

    print(f'请列出所有{quiz_name}!')
    while remaining_items:
        user_input = input(prompt_text)
        total_attempts += 1

        if user_input in remaining_items:
            # 答对且未重复提交
            remaining_items.remove(user_input)
            answered_items.add(user_input)
            print('正确!')
            print(f'已答对:{sorted(answered_items)}')
            correct_count += 1
        elif user_input in answered_items:
            # 重复提交已答对的内容
            print(f'抱歉!{user_input}已经答过了')
        else:
            # 答案错误
            print(f'抱歉!{user_input}不属于{quiz_name}')

    print(f'恭喜你!已经列出所有{quiz_name}。')
    print(f'答对{correct_count}题,共尝试{total_attempts}次')

# 定义所有题目组配置,新增题目只需在这里添加字典即可
quiz_groups = [
    {
        "name": "太阳系行星",
        "items": {'水星', '金星', '地球', '火星'},
        "prompt": "请输入行星名称:"
    },
    {
        "name": "欧盟成员国",
        "items": {'比利时', '保加利亚', '德国', '法国'},
        "prompt": "请输入欧盟成员国名称:"
    },
    {
        "name": "DAX成分公司",
        "items": {'阿迪达斯', '空客', '安联', '巴斯夫'},
        "prompt": "请输入DAX成分公司名称:"
    }
]

# 显示测验选择菜单
print('请选择要参与的测验:')
for idx, quiz in enumerate(quiz_groups, 1):
    print(f'{idx}. {quiz["name"]}')

# 获取并校验用户选择
while True:
    try:
        choice = int(input('输入选项编号:'))
        if 1 <= choice <= len(quiz_groups):
            selected_quiz = quiz_groups[choice-1]
            break
        else:
            print(f'请输入1到{len(quiz_groups)}之间的数字')
    except ValueError:
        print('请输入有效的数字')

# 启动选中的测验
run_quiz(selected_quiz["name"], selected_quiz["items"], selected_quiz["prompt"])

改造说明

  • 逻辑复用:run_quiz函数可适配任意题目组,新增测验无需修改核心逻辑
  • 易于扩展:新增题目组仅需在quiz_groups列表中添加新的配置字典
  • 交互优化:添加菜单选择机制,增加输入校验避免无效操作,已答内容排序展示更清晰
  • 本地化调整:将所有提示文本改为中文,符合使用习惯

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 21:01:52