{"id":1103853,"date":"2025-01-08T16:14:47","date_gmt":"2025-01-08T08:14:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1103853.html"},"modified":"2025-01-08T16:14:49","modified_gmt":"2025-01-08T08:14:49","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%81%9a%e4%b8%80%e4%b8%aa%e7%ad%94%e9%a2%98%e9%a2%98%e5%ba%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1103853.html","title":{"rendered":"\u5982\u4f55\u7528python\u505a\u4e00\u4e2a\u7b54\u9898\u9898\u5e93"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065504\/1ce88cb3-f1d2-41f2-81d5-662f8f2fa500.webp\" alt=\"\u5982\u4f55\u7528python\u505a\u4e00\u4e2a\u7b54\u9898\u9898\u5e93\" \/><\/p>\n<p><p> <strong>\u4e00\u3001\u6838\u5fc3\u89c2\u70b9<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Python\u521b\u5efa\u4e00\u4e2a\u7b54\u9898\u9898\u5e93\u9700\u8981\uff1a\u8bbe\u8ba1\u6570\u636e\u7ed3\u6784\u3001\u5b9e\u73b0\u9898\u5e93\u7ba1\u7406\u3001\u5b9e\u73b0\u9898\u76ee\u5c55\u793a\u548c\u7b54\u9898\u903b\u8f91\u3001\u5b9e\u73b0\u8bc4\u5206\u7cfb\u7edf\u548c\u7edf\u8ba1\u529f\u80fd\u3001\u63d0\u4f9b\u7528\u6237\u754c\u9762<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5b9e\u73b0\u9898\u5e93\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u521b\u5efa\u4e00\u4e2a\u7b54\u9898\u9898\u5e93\u7684\u6838\u5fc3\u5728\u4e8e\u8bbe\u8ba1\u5408\u7406\u7684\u6570\u636e\u7ed3\u6784\u6765\u5b58\u50a8\u9898\u76ee\u3001\u7b54\u6848\u548c\u7528\u6237\u4fe1\u606f\uff0c\u5e76\u5b9e\u73b0\u76f8\u5e94\u7684\u529f\u80fd\u6a21\u5757\u3002\u9898\u5e93\u7ba1\u7406\u662f\u6574\u4e2a\u7cfb\u7edf\u7684\u57fa\u7840\uff0c\u6d89\u53ca\u5230\u9898\u76ee\u7684\u6dfb\u52a0\u3001\u5220\u9664\u3001\u4fee\u6539\u548c\u67e5\u8be2\u7b49\u64cd\u4f5c\u3002\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6216\u6570\u636e\u5e93\u6765\u5b58\u50a8\u9898\u76ee\u6570\u636e\uff0c\u4ee5\u4fbf\u5feb\u901f\u8bbf\u95ee\u548c\u7ba1\u7406\u3002\u5728\u5b9e\u73b0\u9898\u5e93\u7ba1\u7406\u65f6\uff0c\u9996\u5148\u9700\u8981\u5b9a\u4e49\u9898\u76ee\u7684\u6570\u636e\u7ed3\u6784\uff0c\u901a\u5e38\u5305\u62ec\u9898\u76eeID\u3001\u9898\u5e72\u3001\u9009\u9879\u3001\u6b63\u786e\u7b54\u6848\u548c\u5206\u7c7b\u7b49\u5b57\u6bb5\u3002\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u7f16\u5199\u76f8\u5e94\u7684\u51fd\u6570\u6765\u5b9e\u73b0\u9898\u76ee\u7684\u589e\u5220\u6539\u67e5\u64cd\u4f5c\uff0c\u8fd9\u4e9b\u51fd\u6570\u5e94\u5177\u5907\u826f\u597d\u7684\u9c81\u68d2\u6027\u548c\u9519\u8bef\u5904\u7406\u80fd\u529b\uff0c\u4ee5\u786e\u4fdd\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u8bbe\u8ba1\u6570\u636e\u7ed3\u6784<\/strong><\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u5b57\u5178\u5b58\u50a8\u9898\u76ee\u6570\u636e<\/li>\n<\/ol>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u5b58\u50a8\u9898\u76ee\u6570\u636e\u3002\u6bcf\u4e2a\u9898\u76ee\u53ef\u4ee5\u7528\u4e00\u4e2a\u5b57\u5178\u6765\u8868\u793a\uff0c\u5305\u542b\u9898\u76eeID\u3001\u9898\u5e72\u3001\u9009\u9879\u3001\u6b63\u786e\u7b54\u6848\u548c\u5206\u7c7b\u7b49\u4fe1\u606f\u3002\u9898\u76eeID\u53ef\u4ee5\u4f5c\u4e3a\u5b57\u5178\u7684\u952e\uff0c\u9898\u76ee\u4fe1\u606f\u4f5c\u4e3a\u503c\u3002\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u589e\u5220\u6539\u67e5\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">question_bank = {<\/p>\n<p>    1: {<\/p>\n<p>        &quot;question&quot;: &quot;What is the capital of France?&quot;,<\/p>\n<p>        &quot;options&quot;: [&quot;Paris&quot;, &quot;London&quot;, &quot;Berlin&quot;, &quot;Madrid&quot;],<\/p>\n<p>        &quot;answer&quot;: &quot;Paris&quot;,<\/p>\n<p>        &quot;category&quot;: &quot;Geography&quot;<\/p>\n<p>    },<\/p>\n<p>    2: {<\/p>\n<p>        &quot;question&quot;: &quot;What is the largest planet in our solar system?&quot;,<\/p>\n<p>        &quot;options&quot;: [&quot;Earth&quot;, &quot;Mars&quot;, &quot;Jupiter&quot;, &quot;Saturn&quot;],<\/p>\n<p>        &quot;answer&quot;: &quot;Jupiter&quot;,<\/p>\n<p>        &quot;category&quot;: &quot;Astronomy&quot;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u6570\u636e\u5e93\u5b58\u50a8\u9898\u76ee\u6570\u636e<\/li>\n<\/ol>\n<p><p>\u5bf9\u4e8e\u5927\u578b\u9898\u5e93\uff0c\u4f7f\u7528\u6570\u636e\u5e93\u6765\u5b58\u50a8\u9898\u76ee\u6570\u636e\u66f4\u4e3a\u5408\u9002\u3002\u53ef\u4ee5\u9009\u62e9SQLite\u3001MySQL\u6216PostgreSQL\u7b49\u6570\u636e\u5e93\u7cfb\u7edf\u3002\u5728\u6570\u636e\u5e93\u4e2d\uff0c\u53ef\u4ee5\u4e3a\u9898\u76ee\u521b\u5efa\u4e00\u4e2a\u8868\uff0c\u5305\u542b\u9898\u76eeID\u3001\u9898\u5e72\u3001\u9009\u9879\u3001\u6b63\u786e\u7b54\u6848\u548c\u5206\u7c7b\u7b49\u5b57\u6bb5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-sql\">CREATE TABLE questions (<\/p>\n<p>    id INTEGER PRIMARY KEY,<\/p>\n<p>    question TEXT NOT NULL,<\/p>\n<p>    options TEXT NOT NULL,<\/p>\n<p>    answer TEXT NOT NULL,<\/p>\n<p>    category TEXT NOT NULL<\/p>\n<p>);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e09\u3001\u5b9e\u73b0\u9898\u5e93\u7ba1\u7406<\/strong><\/p>\n<\/p>\n<ol>\n<li>\u6dfb\u52a0\u9898\u76ee<\/li>\n<\/ol>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u5411\u9898\u5e93\u4e2d\u6dfb\u52a0\u65b0\u7684\u9898\u76ee\u3002\u51fd\u6570\u63a5\u6536\u9898\u76eeID\u3001\u9898\u5e72\u3001\u9009\u9879\u3001\u6b63\u786e\u7b54\u6848\u548c\u5206\u7c7b\u7b49\u53c2\u6570\uff0c\u5e76\u5c06\u9898\u76ee\u4fe1\u606f\u5b58\u50a8\u5230\u9898\u5e93\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add_question(question_bank, question_id, question, options, answer, category):<\/p>\n<p>    if question_id in question_bank:<\/p>\n<p>        print(&quot;Question ID already exists.&quot;)<\/p>\n<p>        return<\/p>\n<p>    question_bank[question_id] = {<\/p>\n<p>        &quot;question&quot;: question,<\/p>\n<p>        &quot;options&quot;: options,<\/p>\n<p>        &quot;answer&quot;: answer,<\/p>\n<p>        &quot;category&quot;: category<\/p>\n<p>    }<\/p>\n<p>    print(&quot;Question added successfully.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5220\u9664\u9898\u76ee<\/li>\n<\/ol>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u4ece\u9898\u5e93\u4e2d\u5220\u9664\u6307\u5b9a\u7684\u9898\u76ee\u3002\u51fd\u6570\u63a5\u6536\u9898\u76eeID\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u4ece\u9898\u5e93\u4e2d\u79fb\u9664\u8be5\u9898\u76ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def delete_question(question_bank, question_id):<\/p>\n<p>    if question_id not in question_bank:<\/p>\n<p>        print(&quot;Question ID not found.&quot;)<\/p>\n<p>        return<\/p>\n<p>    del question_bank[question_id]<\/p>\n<p>    print(&quot;Question deleted successfully.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u4fee\u6539\u9898\u76ee<\/li>\n<\/ol>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u4fee\u6539\u9898\u5e93\u4e2d\u7684\u9898\u76ee\u3002\u51fd\u6570\u63a5\u6536\u9898\u76eeID\u3001\u9898\u5e72\u3001\u9009\u9879\u3001\u6b63\u786e\u7b54\u6848\u548c\u5206\u7c7b\u7b49\u53c2\u6570\uff0c\u5e76\u66f4\u65b0\u9898\u5e93\u4e2d\u7684\u9898\u76ee\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def update_question(question_bank, question_id, question=None, options=None, answer=None, category=None):<\/p>\n<p>    if question_id not in question_bank:<\/p>\n<p>        print(&quot;Question ID not found.&quot;)<\/p>\n<p>        return<\/p>\n<p>    if question:<\/p>\n<p>        question_bank[question_id][&quot;question&quot;] = question<\/p>\n<p>    if options:<\/p>\n<p>        question_bank[question_id][&quot;options&quot;] = options<\/p>\n<p>    if answer:<\/p>\n<p>        question_bank[question_id][&quot;answer&quot;] = answer<\/p>\n<p>    if category:<\/p>\n<p>        question_bank[question_id][&quot;category&quot;] = category<\/p>\n<p>    print(&quot;Question updated successfully.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li>\u67e5\u8be2\u9898\u76ee<\/li>\n<\/ol>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u6839\u636e\u9898\u76eeID\u67e5\u8be2\u9898\u5e93\u4e2d\u7684\u9898\u76ee\u3002\u51fd\u6570\u63a5\u6536\u9898\u76eeID\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u9898\u76ee\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_question(question_bank, question_id):<\/p>\n<p>    if question_id not in question_bank:<\/p>\n<p>        print(&quot;Question ID not found.&quot;)<\/p>\n<p>        return None<\/p>\n<p>    return question_bank[question_id]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u56db\u3001\u5b9e\u73b0\u9898\u76ee\u5c55\u793a\u548c\u7b54\u9898\u903b\u8f91<\/strong><\/p>\n<\/p>\n<ol>\n<li>\u663e\u793a\u9898\u76ee<\/li>\n<\/ol>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u663e\u793a\u9898\u5e93\u4e2d\u7684\u9898\u76ee\u3002\u51fd\u6570\u63a5\u6536\u9898\u76eeID\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5728\u63a7\u5236\u53f0\u8f93\u51fa\u9898\u5e72\u548c\u9009\u9879\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def display_question(question_bank, question_id):<\/p>\n<p>    question = get_question(question_bank, question_id)<\/p>\n<p>    if question is None:<\/p>\n<p>        return<\/p>\n<p>    print(f&quot;Question: {question[&#39;question&#39;]}&quot;)<\/p>\n<p>    for idx, option in enumerate(question[&#39;options&#39;], 1):<\/p>\n<p>        print(f&quot;{idx}. {option}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5b9e\u73b0\u7b54\u9898\u903b\u8f91<\/li>\n<\/ol>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u5b9e\u73b0\u7528\u6237\u7b54\u9898\u7684\u903b\u8f91\u3002\u51fd\u6570\u63a5\u6536\u9898\u76eeID\u548c\u7528\u6237\u7b54\u6848\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5224\u65ad\u7528\u6237\u7b54\u6848\u662f\u5426\u6b63\u786e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def answer_question(question_bank, question_id, user_answer):<\/p>\n<p>    question = get_question(question_bank, question_id)<\/p>\n<p>    if question is None:<\/p>\n<p>        return False<\/p>\n<p>    correct_answer = question[&#39;answer&#39;]<\/p>\n<p>    if user_answer == correct_answer:<\/p>\n<p>        print(&quot;Correct!&quot;)<\/p>\n<p>        return True<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Incorrect. The correct answer is {correct_answer}.&quot;)<\/p>\n<p>        return False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e94\u3001\u5b9e\u73b0\u8bc4\u5206\u7cfb\u7edf\u548c\u7edf\u8ba1\u529f\u80fd<\/strong><\/p>\n<\/p>\n<ol>\n<li>\u8bc4\u5206\u7cfb\u7edf<\/li>\n<\/ol>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u8ba1\u7b97\u7528\u6237\u7684\u7b54\u9898\u5f97\u5206\u3002\u51fd\u6570\u63a5\u6536\u7528\u6237\u7684\u7b54\u9898\u8bb0\u5f55\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8ba1\u7b97\u7528\u6237\u7684\u603b\u5206\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_score(answer_records):<\/p>\n<p>    score = 0<\/p>\n<p>    for record in answer_records:<\/p>\n<p>        if record[&#39;correct&#39;]:<\/p>\n<p>            score += 1<\/p>\n<p>    return score<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u7edf\u8ba1\u529f\u80fd<\/li>\n<\/ol>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u7edf\u8ba1\u7528\u6237\u7684\u7b54\u9898\u60c5\u51b5\u3002\u51fd\u6570\u63a5\u6536\u7528\u6237\u7684\u7b54\u9898\u8bb0\u5f55\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u7edf\u8ba1\u7528\u6237\u7684\u7b54\u9898\u6b63\u786e\u7387\u548c\u9519\u8bef\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_statistics(answer_records):<\/p>\n<p>    total_questions = len(answer_records)<\/p>\n<p>    correct_answers = sum(1 for record in answer_records if record[&#39;correct&#39;])<\/p>\n<p>    incorrect_answers = total_questions - correct_answers<\/p>\n<p>    correct_rate = correct_answers \/ total_questions * 100<\/p>\n<p>    incorrect_rate = incorrect_answers \/ total_questions * 100<\/p>\n<p>    return {<\/p>\n<p>        &quot;total_questions&quot;: total_questions,<\/p>\n<p>        &quot;correct_answers&quot;: correct_answers,<\/p>\n<p>        &quot;incorrect_answers&quot;: incorrect_answers,<\/p>\n<p>        &quot;correct_rate&quot;: correct_rate,<\/p>\n<p>        &quot;incorrect_rate&quot;: incorrect_rate<\/p>\n<p>    }<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u516d\u3001\u63d0\u4f9b\u7528\u6237\u754c\u9762<\/strong><\/p>\n<\/p>\n<ol>\n<li>\u547d\u4ee4\u884c\u754c\u9762<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4\u884c\u754c\u9762\uff08CLI\uff09\u6765\u4e0e\u7528\u6237\u8fdb\u884c\u4ea4\u4e92\uff0c\u5b9e\u73b0\u9898\u5e93\u7ba1\u7406\u3001\u9898\u76ee\u5c55\u793a\u548c\u7b54\u9898\u7b49\u529f\u80fd\u3002\u53ef\u4ee5\u4f7f\u7528<code>argparse<\/code>\u6a21\u5757\u6765\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u5b9e\u73b0\u4e0d\u540c\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import argparse<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    question_bank = {}<\/p>\n<p>    answer_records = []<\/p>\n<p>    parser = argparse.ArgumentParser(description=&quot;Quiz System&quot;)<\/p>\n<p>    parser.add_argument(&quot;action&quot;, choices=[&quot;add&quot;, &quot;delete&quot;, &quot;update&quot;, &quot;display&quot;, &quot;answer&quot;, &quot;score&quot;, &quot;stats&quot;], help=&quot;Action to perform&quot;)<\/p>\n<p>    parser.add_argument(&quot;--id&quot;, type=int, help=&quot;Question ID&quot;)<\/p>\n<p>    parser.add_argument(&quot;--question&quot;, help=&quot;Question text&quot;)<\/p>\n<p>    parser.add_argument(&quot;--options&quot;, nargs=&quot;+&quot;, help=&quot;Question options&quot;)<\/p>\n<p>    parser.add_argument(&quot;--answer&quot;, help=&quot;Correct answer&quot;)<\/p>\n<p>    parser.add_argument(&quot;--category&quot;, help=&quot;Question category&quot;)<\/p>\n<p>    parser.add_argument(&quot;--user_answer&quot;, help=&quot;User answer&quot;)<\/p>\n<p>    args = parser.parse_args()<\/p>\n<p>    if args.action == &quot;add&quot;:<\/p>\n<p>        add_question(question_bank, args.id, args.question, args.options, args.answer, args.category)<\/p>\n<p>    elif args.action == &quot;delete&quot;:<\/p>\n<p>        delete_question(question_bank, args.id)<\/p>\n<p>    elif args.action == &quot;update&quot;:<\/p>\n<p>        update_question(question_bank, args.id, args.question, args.options, args.answer, args.category)<\/p>\n<p>    elif args.action == &quot;display&quot;:<\/p>\n<p>        display_question(question_bank, args.id)<\/p>\n<p>    elif args.action == &quot;answer&quot;:<\/p>\n<p>        correct = answer_question(question_bank, args.id, args.user_answer)<\/p>\n<p>        answer_records.append({&quot;question_id&quot;: args.id, &quot;correct&quot;: correct})<\/p>\n<p>    elif args.action == &quot;score&quot;:<\/p>\n<p>        score = calculate_score(answer_records)<\/p>\n<p>        print(f&quot;Your score: {score}&quot;)<\/p>\n<p>    elif args.action == &quot;stats&quot;:<\/p>\n<p>        stats = calculate_statistics(answer_records)<\/p>\n<p>        print(f&quot;Total Questions: {stats[&#39;total_questions&#39;]}&quot;)<\/p>\n<p>        print(f&quot;Correct Answers: {stats[&#39;correct_answers&#39;]}&quot;)<\/p>\n<p>        print(f&quot;Incorrect Answers: {stats[&#39;incorrect_answers&#39;]}&quot;)<\/p>\n<p>        print(f&quot;Correct Rate: {stats[&#39;correct_rate&#39;]:.2f}%&quot;)<\/p>\n<p>        print(f&quot;Incorrect Rate: {stats[&#39;incorrect_rate&#39;]:.2f}%&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u56fe\u5f62\u7528\u6237\u754c\u9762<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Tkinter\u7b49\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e93\u6765\u5b9e\u73b0\u66f4\u4e3a\u53cb\u597d\u7684\u7528\u6237\u754c\u9762\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Tkinter\u793a\u4f8b\uff0c\u7528\u4e8e\u5b9e\u73b0\u9898\u76ee\u5c55\u793a\u548c\u7b54\u9898\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import messagebox<\/p>\n<p>class QuizApp:<\/p>\n<p>    def __init__(self, root, question_bank):<\/p>\n<p>        self.root = root<\/p>\n<p>        self.question_bank = question_bank<\/p>\n<p>        self.current_question_id = None<\/p>\n<p>        self.score = 0<\/p>\n<p>        self.question_label = tk.Label(root, text=&quot;&quot;)<\/p>\n<p>        self.question_label.pack()<\/p>\n<p>        self.option_vars = []<\/p>\n<p>        self.option_buttons = []<\/p>\n<p>        for i in range(4):<\/p>\n<p>            var = tk.StringVar()<\/p>\n<p>            button = tk.Radiobutton(root, text=&quot;&quot;, variable=var, value=&quot;&quot;, command=self.check_answer)<\/p>\n<p>            button.pack()<\/p>\n<p>            self.option_vars.append(var)<\/p>\n<p>            self.option_buttons.append(button)<\/p>\n<p>        self.next_button = tk.Button(root, text=&quot;Next&quot;, command=self.next_question)<\/p>\n<p>        self.next_button.pack()<\/p>\n<p>    def display_question(self, question_id):<\/p>\n<p>        self.current_question_id = question_id<\/p>\n<p>        question = self.question_bank[question_id]<\/p>\n<p>        self.question_label.config(text=question[&#39;question&#39;])<\/p>\n<p>        for idx, option in enumerate(question[&#39;options&#39;]):<\/p>\n<p>            self.option_vars[idx].set(option)<\/p>\n<p>            self.option_buttons[idx].config(text=option, value=option)<\/p>\n<p>    def check_answer(self):<\/p>\n<p>        selected_option = None<\/p>\n<p>        for var in self.option_vars:<\/p>\n<p>            if var.get():<\/p>\n<p>                selected_option = var.get()<\/p>\n<p>                break<\/p>\n<p>        correct_answer = self.question_bank[self.current_question_id][&#39;answer&#39;]<\/p>\n<p>        if selected_option == correct_answer:<\/p>\n<p>            messagebox.showinfo(&quot;Result&quot;, &quot;Correct!&quot;)<\/p>\n<p>            self.score += 1<\/p>\n<p>        else:<\/p>\n<p>            messagebox.showinfo(&quot;Result&quot;, f&quot;Incorrect. The correct answer is {correct_answer}.&quot;)<\/p>\n<p>    def next_question(self):<\/p>\n<p>        next_question_id = self.current_question_id + 1<\/p>\n<p>        if next_question_id in self.question_bank:<\/p>\n<p>            self.display_question(next_question_id)<\/p>\n<p>        else:<\/p>\n<p>            messagebox.showinfo(&quot;Quiz Completed&quot;, f&quot;Your score: {self.score}&quot;)<\/p>\n<p>            self.root.quit()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    question_bank = {<\/p>\n<p>        1: {<\/p>\n<p>            &quot;question&quot;: &quot;What is the capital of France?&quot;,<\/p>\n<p>            &quot;options&quot;: [&quot;Paris&quot;, &quot;London&quot;, &quot;Berlin&quot;, &quot;Madrid&quot;],<\/p>\n<p>            &quot;answer&quot;: &quot;Paris&quot;,<\/p>\n<p>            &quot;category&quot;: &quot;Geography&quot;<\/p>\n<p>        },<\/p>\n<p>        2: {<\/p>\n<p>            &quot;question&quot;: &quot;What is the largest planet in our solar system?&quot;,<\/p>\n<p>            &quot;options&quot;: [&quot;Earth&quot;, &quot;Mars&quot;, &quot;Jupiter&quot;, &quot;Saturn&quot;],<\/p>\n<p>            &quot;answer&quot;: &quot;Jupiter&quot;,<\/p>\n<p>            &quot;category&quot;: &quot;Astronomy&quot;<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>    root = tk.Tk()<\/p>\n<p>    app = QuizApp(root, question_bank)<\/p>\n<p>    app.display_question(1)<\/p>\n<p>    root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u5185\u5bb9\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u4f7f\u7528Python\u521b\u5efa\u4e00\u4e2a\u7b54\u9898\u9898\u5e93\u6240\u9700\u7684\u5404\u4e2a\u6b65\u9aa4\u548c\u5b9e\u73b0\u65b9\u6cd5\u3002\u901a\u8fc7\u8bbe\u8ba1\u5408\u7406\u7684\u6570\u636e\u7ed3\u6784\u3001\u5b9e\u73b0\u9898\u5e93\u7ba1\u7406\u3001\u9898\u76ee\u5c55\u793a\u548c\u7b54\u9898\u903b\u8f91\uff0c\u4ee5\u53ca\u63d0\u4f9b\u7528\u6237\u754c\u9762\uff0c\u53ef\u4ee5\u6784\u5efa\u4e00\u4e2a\u529f\u80fd\u5b8c\u5584\u7684\u7b54\u9898\u7cfb\u7edf\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5f00\u59cb\u4f7f\u7528Python\u6784\u5efa\u4e00\u4e2a\u7b54\u9898\u9898\u5e93\uff1f<\/strong><br \/>\u8981\u5f00\u59cb\u6784\u5efa\u4e00\u4e2a\u7b54\u9898\u9898\u5e93\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684\u57fa\u7840\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u5b57\u5178\u548c\u5217\u8868\uff0c\u6765\u5b58\u50a8\u95ee\u9898\u53ca\u5176\u7b54\u6848\u3002\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6587\u4ef6\u8bfb\u53d6\u548c\u5199\u5165\u529f\u80fd\uff0c\u5c06\u9898\u5e93\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u7ba1\u7406\u548c\u66f4\u65b0\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u6a21\u5757\u5982<code>json<\/code>\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u9898\u5e93\u6570\u636e\u7684\u5b58\u50a8\u548c\u8bfb\u53d6\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5e93\u548c\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u6211\u5728Python\u4e2d\u521b\u5efa\u7b54\u9898\u9898\u5e93\uff1f<\/strong><br \/>\u5728\u521b\u5efa\u7b54\u9898\u9898\u5e93\u65f6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u6d41\u884c\u7684Python\u5e93\uff0c\u4f8b\u5982<code>Flask<\/code>\u6216<code>Django<\/code>\u6765\u6784\u5efaWeb\u5e94\u7528\uff0c\u65b9\u4fbf\u7528\u6237\u5728\u7ebf\u7b54\u9898\u3002\u5bf9\u4e8e\u6570\u636e\u5904\u7406\uff0c<code>pandas<\/code>\u53ef\u4ee5\u5e2e\u52a9\u60a8\u7ba1\u7406\u548c\u5206\u6790\u9898\u5e93\u6570\u636e\uff0c\u800c<code>SQLite<\/code>\u6216<code>SQLAlchemy<\/code>\u53ef\u4ee5\u7528\u4e8e\u6570\u636e\u5e93\u7ba1\u7406\uff0c\u4ee5\u4fbf\u66f4\u9ad8\u6548\u5730\u5b58\u50a8\u548c\u67e5\u8be2\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u6211\u7684\u7b54\u9898\u9898\u5e93\u5177\u6709\u826f\u597d\u7684\u7528\u6237\u4f53\u9a8c\uff1f<\/strong><br \/>\u4e3a\u4e86\u63d0\u4f9b\u826f\u597d\u7684\u7528\u6237\u4f53\u9a8c\uff0c\u60a8\u53ef\u4ee5\u8003\u8651\u8bbe\u8ba1\u4e00\u4e2a\u7b80\u6d01\u76f4\u89c2\u7684\u7528\u6237\u754c\u9762\uff0c\u5e76\u786e\u4fdd\u9898\u5e93\u4e2d\u7684\u95ee\u9898\u5206\u7c7b\u6e05\u6670\u3002\u63d0\u4f9b\u53cd\u9988\u673a\u5236\uff0c\u4f8b\u5982\u7b54\u9898\u540e\u663e\u793a\u6b63\u786e\u7b54\u6848\u548c\u89e3\u6790\uff0c\u4e5f\u80fd\u63d0\u5347\u7528\u6237\u7684\u5b66\u4e60\u4f53\u9a8c\u3002\u6b64\u5916\uff0c\u8003\u8651\u5b9e\u73b0\u968f\u673a\u51fa\u9898\u3001\u8ba1\u65f6\u7b54\u9898\u7b49\u529f\u80fd\uff0c\u8fd9\u6837\u53ef\u4ee5\u589e\u52a0\u8da3\u5473\u6027\u548c\u6311\u6218\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e00\u3001\u6838\u5fc3\u89c2\u70b9 \u4f7f\u7528Python\u521b\u5efa\u4e00\u4e2a\u7b54\u9898\u9898\u5e93\u9700\u8981\uff1a\u8bbe\u8ba1\u6570\u636e\u7ed3\u6784\u3001\u5b9e\u73b0\u9898\u5e93\u7ba1\u7406\u3001\u5b9e\u73b0\u9898\u76ee\u5c55\u793a\u548c\u7b54\u9898\u903b\u8f91\u3001\u5b9e\u73b0\u8bc4 [&hellip;]","protected":false},"author":3,"featured_media":1103863,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1103853"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1103853"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1103853\/revisions"}],"predecessor-version":[{"id":1103865,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1103853\/revisions\/1103865"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1103863"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1103853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1103853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1103853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}