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

如何用无第三方库的Python实现可收集答案的.exe调查程序?

Hey there! I've adjusted your code to add data storage functionality, kept it completely free of third-party libraries, and included steps to package it as an .exe for easy distribution to your 50 participants. Here's the breakdown:

解决方案:无第三方库的Python调查工具(可打包为.exe)

核心改进后的完整代码

# 获取用户姓名
print("Enter your name:")
name = input().strip()

# 获取参与的团队数量(添加输入验证)
print("How many teams do you play for?")
while True:
    try:
        number_of_teams = int(input().strip())
        if number_of_teams > 0:
            break
        else:
            print("Please enter a positive number.")
    except ValueError:
        print("Invalid input. Please enter a number.")

print(f"You have to fill this form {number_of_teams} times.\n")

# 调查问题列表
questions_list = [
    "Q1. A?",
    "Q2. B?",
    "Q3. C?",
    "Q4. D?",
    "Q5. E?",
    "Q6. F?",
    "Q7. G?"
]

# 对应问题的选项列表
answers_list = [
    ["A.1", "B.2", "C.3", "D.4"],
    ["A.1", "B.2", "C.3", "D.4"],
    ["A.1", "B.2", "C.3", "D.4"],
    ["A.1", "B.2", "C.3", "D.4"],
    ["A.1", "B.2", "C.3", "D.4"],
    ["A.1", "B.2", "C.3", "D.4"],
    ["A.1", "B.2", "C.3", "D.4"]
]

# 数据存储函数(原生文件操作,无第三方库)
def save_response(name, team, responses):
    # 以追加模式打开文件,不存在则自动创建
    with open("survey_results.txt", "a", encoding="utf-8") as f:
        # 写入格式:姓名,团队,Q1答案,Q2答案,...Q7答案
        data_line = f"{name},{team},{','.join(responses)}\n"
        f.write(data_line)

# 循环处理每个团队的作答
for _ in range(number_of_teams):
    print("Enter your team name:")
    team = input().strip()
    
    # 存储当前团队的所有答案
    current_responses = []
    
    print(f"\n--- Starting survey for team: {team} ---\n")
    for question, options in zip(questions_list, answers_list):
        print(question)
        # 格式化打印选项,更易读
        for opt in options:
            print(f"  {opt}")
        # 验证答案输入有效性
        while True:
            answer = input("Enter your choice (A/B/C/D): ").strip().upper()
            if answer in ["A", "B", "C", "D"]:
                current_responses.append(answer)
                break
            else:
                print("Invalid choice. Please enter A, B, C, or D.")
        print("\n")
    
    # 保存当前团队的作答数据
    save_response(name, team, current_responses)
    print(f"--- Survey for team {team} completed. Responses saved! ---\n")

print("All surveys completed! Thank you for your participation.")

关键改进说明

  • 数据存储:用Python原生文件操作将所有作答写入survey_results.txt,采用逗号分隔格式,方便后续用基础文本工具做简单统计
  • 输入验证:添加了数字输入和答案选项的校验,避免无效数据混入
  • 用户体验优化:调整了选项排版,让问卷更易读;添加了进度提示,让用户清楚当前作答状态
  • 零第三方库依赖:所有功能均使用Python标准库实现,完全符合你的数据共享要求

打包为.exe文件

虽然Python标准库没有自带打包工具,pyinstaller是轻量且常用的打包工具(仅用于打包,不影响数据处理逻辑),步骤如下:

  1. 安装pyinstaller:pip install pyinstaller
  2. 在代码所在文件夹打开命令行,运行:pyinstaller --onefile --windowed your_script_name.py
    • --onefile:生成单个.exe文件,方便分发
    • --windowed:隐藏命令行窗口(如果需要保留命令行交互,去掉该参数)
  3. 生成的.exe文件会在dist文件夹中,直接分发给参与者即可

基础数据分析示例(无第三方库)

如果需要做简单统计,比如统计Q1选A的人数,可使用这段原生代码:

# 统计Q1选A的次数
count_a = 0
with open("survey_results.txt", "r", encoding="utf-8") as f:
    for line in f:
        parts = line.strip().split(",")
        if parts[2] == "A":  # Q1答案是第3个元素(索引2)
            count_a += 1
print(f"Q1选A的人数:{count_a}")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:48:52