{"id":1176710,"date":"2025-01-15T17:48:24","date_gmt":"2025-01-15T09:48:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1176710.html"},"modified":"2025-01-15T17:48:27","modified_gmt":"2025-01-15T09:48:27","slug":"python%e5%a6%82%e4%bd%95%e7%bc%96%e5%86%99%e5%a4%9a%e5%8a%9f%e8%83%bd%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1176710.html","title":{"rendered":"python\u5982\u4f55\u7f16\u5199\u591a\u529f\u80fd\u7a0b\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25111828\/30287158-f4a6-4c8c-a123-dfc06d6f70e4.webp\" alt=\"python\u5982\u4f55\u7f16\u5199\u591a\u529f\u80fd\u7a0b\u5e8f\" \/><\/p>\n<p><p> <strong>\u7f16\u5199\u591a\u529f\u80fdPython\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u6a21\u5757\u5316\u8bbe\u8ba1\u3001\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u3001\u591a\u7ebf\u7a0b\u5904\u7406\u3001\u9519\u8bef\u5904\u7406\u548c\u826f\u597d\u7684\u6587\u6863\u5316\u6765\u5b9e\u73b0\u3002\u8fd9\u4e9b\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u5347\u7a0b\u5e8f\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u8fd8\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u529f\u80fd\u6027\u548c\u6548\u7387\u3002<\/strong> \u5176\u4e2d\uff0c\u6a21\u5757\u5316\u8bbe\u8ba1\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u65b9\u9762\uff0c\u901a\u8fc7\u5c06\u7a0b\u5e8f\u5206\u89e3\u6210\u591a\u4e2a\u72ec\u7acb\u7684\u6a21\u5757\uff0c\u6bcf\u4e2a\u6a21\u5757\u8d1f\u8d23\u7279\u5b9a\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u548c\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h4>\u6a21\u5757\u5316\u8bbe\u8ba1\u8be6\u89e3<\/h4>\n<\/p>\n<p><p>\u6a21\u5757\u5316\u8bbe\u8ba1\u662f\u6307\u5c06\u4e00\u4e2a\u590d\u6742\u7684\u7a0b\u5e8f\u5206\u89e3\u6210\u591a\u4e2a\u76f8\u5bf9\u72ec\u7acb\u7684\u6a21\u5757\uff0c\u6bcf\u4e2a\u6a21\u5757\u8d1f\u8d23\u7279\u5b9a\u7684\u529f\u80fd\uff0c\u8fd9\u6837\u53ef\u4ee5\u4f7f\u7a0b\u5e8f\u7684\u7ed3\u6784\u66f4\u52a0\u6e05\u6670\uff0c\u4ee3\u7801\u66f4\u52a0\u6613\u4e8e\u7ef4\u62a4\u548c\u6269\u5c55\u3002\u6a21\u5757\u5316\u8bbe\u8ba1\u7684\u4f18\u52bf\u5728\u4e8e\uff1a<strong>\u964d\u4f4e\u4e86\u4ee3\u7801\u7684\u8026\u5408\u5ea6\u3001\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u590d\u7528\u6027\u3001\u65b9\u4fbf\u8c03\u8bd5\u548c\u6d4b\u8bd5\u3001\u589e\u5f3a\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6a21\u5757\u5316\u8bbe\u8ba1<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u591a\u4e2a.py\u6587\u4ef6\u6765\u5b9a\u4e49\u4e0d\u540c\u7684\u6a21\u5757\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u5e93\u64cd\u4f5c\u76f8\u5173\u7684\u4ee3\u7801\u653e\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d\uff0c\u7528\u6237\u754c\u9762\u76f8\u5173\u7684\u4ee3\u7801\u653e\u5728\u53e6\u4e00\u4e2a\u6a21\u5757\u4e2d\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># database.py<\/p>\n<p>def connect_to_database():<\/p>\n<p>    print(&quot;Connecting to the database...&quot;)<\/p>\n<p>def query_database(query):<\/p>\n<p>    print(f&quot;Querying the database with: {query}&quot;)<\/p>\n<h2><strong>ui.py<\/strong><\/h2>\n<p>def display_menu():<\/p>\n<p>    print(&quot;Displaying menu...&quot;)<\/p>\n<p>def get_user_input():<\/p>\n<p>    return input(&quot;Enter your choice: &quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4e3b\u7a0b\u5e8f\u8c03\u7528\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u5728\u4e3b\u7a0b\u5e8f\u4e2d\uff0c\u901a\u8fc7import\u8bed\u53e5\u6765\u5bfc\u5165\u8fd9\u4e9b\u6a21\u5757\uff0c\u5e76\u8c03\u7528\u5176\u4e2d\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/p>\n<p>import database<\/p>\n<p>import ui<\/p>\n<p>def main():<\/p>\n<p>    database.connect_to_database()<\/p>\n<p>    ui.display_menu()<\/p>\n<p>    choice = ui.get_user_input()<\/p>\n<p>    database.query_database(choice)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b<\/h3>\n<\/p>\n<p><p>\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\u662f\u4e00\u79cd\u7a0b\u5e8f\u8bbe\u8ba1\u8303\u5f0f\uff0c\u5b83\u901a\u8fc7\u5b9a\u4e49\u7c7b\u548c\u5bf9\u8c61\u6765\u5b9e\u73b0\u7a0b\u5e8f\u7684\u6a21\u5757\u5316\u548c\u590d\u7528\u6027\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u7c7b\u548c\u521b\u5efa\u5bf9\u8c61\u6765\u5b9e\u73b0OOP\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u7c7b\u548c\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>Person<\/code>\u7c7b\u548c\u4e00\u4e2a<code>Employee<\/code>\u7c7b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>    def display_info(self):<\/p>\n<p>        print(f&quot;Name: {self.name}, Age: {self.age}&quot;)<\/p>\n<p>class Employee(Person):<\/p>\n<p>    def __init__(self, name, age, employee_id):<\/p>\n<p>        super().__init__(name, age)<\/p>\n<p>        self.employee_id = employee_id<\/p>\n<p>    def display_info(self):<\/p>\n<p>        super().display_info()<\/p>\n<p>        print(f&quot;Employee ID: {self.employee_id}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u548c\u4f7f\u7528\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u5728\u4e3b\u7a0b\u5e8f\u4e2d\uff0c\u53ef\u4ee5\u521b\u5efa\u8fd9\u4e9b\u7c7b\u7684\u5bf9\u8c61\uff0c\u5e76\u8c03\u7528\u5b83\u4eec\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def main():<\/p>\n<p>    person = Person(&quot;Alice&quot;, 30)<\/p>\n<p>    person.display_info()<\/p>\n<p>    employee = Employee(&quot;Bob&quot;, 40, &quot;E123&quot;)<\/p>\n<p>    employee.display_info()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u591a\u7ebf\u7a0b\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u591a\u7ebf\u7a0b\u5904\u7406\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u548c\u54cd\u5e94\u901f\u5ea6\u3002Python\u63d0\u4f9b\u4e86<code>threading<\/code>\u6a21\u5757\u6765\u652f\u6301\u591a\u7ebf\u7a0b\u7f16\u7a0b\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u7ebf\u7a0b<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f<code>threading.Thread<\/code>\u7c7b\u6765\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u7ebf\u7a0b\u7c7b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>class MyThread(threading.Thread):<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.name = name<\/p>\n<p>    def run(self):<\/p>\n<p>        print(f&quot;Thread {self.name} is running&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u542f\u52a8\u7ebf\u7a0b<\/h4>\n<\/p>\n<p><p>\u5728\u4e3b\u7a0b\u5e8f\u4e2d\uff0c\u53ef\u4ee5\u521b\u5efa\u5e76\u542f\u52a8\u591a\u4e2a\u7ebf\u7a0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def main():<\/p>\n<p>    thread1 = MyThread(&quot;A&quot;)<\/p>\n<p>    thread2 = MyThread(&quot;B&quot;)<\/p>\n<p>    thread1.start()<\/p>\n<p>    thread2.start()<\/p>\n<p>    thread1.join()<\/p>\n<p>    thread2.join()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u9519\u8bef\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u9519\u8bef\u5904\u7406\u662f\u7f16\u5199\u5065\u58ee\u7a0b\u5e8f\u7684\u91cd\u8981\u90e8\u5206\u3002Python\u63d0\u4f9b\u4e86<code>try-except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6355\u83b7\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def divide(a, b):<\/p>\n<p>    try:<\/p>\n<p>        result = a \/ b<\/p>\n<p>    except ZeroDivisionError:<\/p>\n<p>        print(&quot;Error: Division by zero is not allowed.&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Result: {result}&quot;)<\/p>\n<p>def main():<\/p>\n<p>    divide(10, 2)<\/p>\n<p>    divide(10, 0)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u629b\u51fa\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>raise<\/code>\u8bed\u53e5\u6765\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class CustomError(Exception):<\/p>\n<p>    pass<\/p>\n<p>def risky_operation():<\/p>\n<p>    raise CustomError(&quot;Something went wrong!&quot;)<\/p>\n<p>def main():<\/p>\n<p>    try:<\/p>\n<p>        risky_operation()<\/p>\n<p>    except CustomError as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u826f\u597d\u7684\u6587\u6863\u5316<\/h3>\n<\/p>\n<p><p>\u826f\u597d\u7684\u6587\u6863\u5316\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002Python\u63d0\u4f9b\u4e86\u6587\u6863\u5b57\u7b26\u4e32\uff08docstring\uff09\u6765\u7f16\u5199\u4ee3\u7801\u7684\u6587\u6863\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6587\u6863\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4e09\u5f15\u53f7\uff08<code>&quot;&quot;&quot;<\/code>\uff09\u6765\u7f16\u5199\u6587\u6863\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    Adds two numbers.<\/p>\n<p>    Parameters:<\/p>\n<p>    a (int): The first number.<\/p>\n<p>    b (int): The second number.<\/p>\n<p>    Returns:<\/p>\n<p>    int: The sum of the two numbers.<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    return a + b<\/p>\n<p>def main():<\/p>\n<p>    print(add(2, 3))<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u751f\u6210\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\uff08\u5982Sphinx\uff09\u6765\u81ea\u52a8\u751f\u6210\u4ee3\u7801\u7684\u6587\u6863\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install sphinx<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u521b\u5efaSphinx\u9879\u76ee\u5e76\u751f\u6210\u6587\u6863\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sphinx-quickstart<\/p>\n<p>sphinx-apidoc -o docs\/ .<\/p>\n<p>make html<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u793a\u4f8b\u9879\u76ee\uff1a\u56fe\u4e66\u7ba1\u7406\u7cfb\u7edf<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u4e0a\u8ff0\u5185\u5bb9\uff0c\u6211\u4eec\u5c06\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u4e66\u7ba1\u7406\u7cfb\u7edf\u793a\u4f8b\u6765\u5c55\u793a\u5982\u4f55\u7f16\u5199\u4e00\u4e2a\u591a\u529f\u80fd\u7684Python\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6a21\u5757\u5b9a\u4e49<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u6570\u636e\u5e93\u64cd\u4f5c\u6a21\u5757\u3001\u7528\u6237\u754c\u9762\u6a21\u5757\u548c\u4e1a\u52a1\u903b\u8f91\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># database.py<\/p>\n<p>books = []<\/p>\n<p>def add_book(book):<\/p>\n<p>    books.append(book)<\/p>\n<p>def get_books():<\/p>\n<p>    return books<\/p>\n<h2><strong>ui.py<\/strong><\/h2>\n<p>def display_menu():<\/p>\n<p>    print(&quot;1. Add Book&quot;)<\/p>\n<p>    print(&quot;2. View Books&quot;)<\/p>\n<p>    print(&quot;3. Exit&quot;)<\/p>\n<p>def get_user_input(prompt):<\/p>\n<p>    return input(prompt)<\/p>\n<h2><strong>logic.py<\/strong><\/h2>\n<p>import database<\/p>\n<p>import ui<\/p>\n<p>def add_book():<\/p>\n<p>    title = ui.get_user_input(&quot;Enter book title: &quot;)<\/p>\n<p>    author = ui.get_user_input(&quot;Enter book author: &quot;)<\/p>\n<p>    database.add_book({&quot;title&quot;: title, &quot;author&quot;: author})<\/p>\n<p>def view_books():<\/p>\n<p>    books = database.get_books()<\/p>\n<p>    if not books:<\/p>\n<p>        print(&quot;No books found.&quot;)<\/p>\n<p>    else:<\/p>\n<p>        for book in books:<\/p>\n<p>            print(f&quot;Title: {book[&#39;title&#39;]}, Author: {book[&#39;author&#39;]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4e3b\u7a0b\u5e8f<\/h4>\n<\/p>\n<p><p>\u5728\u4e3b\u7a0b\u5e8f\u4e2d\uff0c\u5bfc\u5165\u8fd9\u4e9b\u6a21\u5757\u5e76\u5b9e\u73b0\u4e3b\u903b\u8f91\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import ui<\/p>\n<p>import logic<\/p>\n<p>def main():<\/p>\n<p>    while True:<\/p>\n<p>        ui.display_menu()<\/p>\n<p>        choice = ui.get_user_input(&quot;Enter your choice: &quot;)<\/p>\n<p>        if choice == &quot;1&quot;:<\/p>\n<p>            logic.add_book()<\/p>\n<p>        elif choice == &quot;2&quot;:<\/p>\n<p>            logic.view_books()<\/p>\n<p>        elif choice == &quot;3&quot;:<\/p>\n<p>            break<\/p>\n<p>        else:<\/p>\n<p>            print(&quot;Invalid choice. Please try again.&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u6269\u5c55\u529f\u80fd<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u8fdb\u4e00\u6b65\u6269\u5c55\u56fe\u4e66\u7ba1\u7406\u7cfb\u7edf\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u6dfb\u52a0\u591a\u7ebf\u7a0b\u5904\u7406\u3001\u9519\u8bef\u5904\u7406\u548c\u6587\u6863\u5316\u3002<\/p>\n<\/p>\n<p><h5>\u591a\u7ebf\u7a0b\u5904\u7406<\/h5>\n<\/p>\n<p><p>\u4f7f\u7528\u591a\u7ebf\u7a0b\u5904\u7406\u6765\u63d0\u9ad8\u7a0b\u5e8f\u7684\u54cd\u5e94\u901f\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>class AddBookThread(threading.Thread):<\/p>\n<p>    def run(self):<\/p>\n<p>        logic.add_book()<\/p>\n<p>def main():<\/p>\n<p>    while True:<\/p>\n<p>        ui.display_menu()<\/p>\n<p>        choice = ui.get_user_input(&quot;Enter your choice: &quot;)<\/p>\n<p>        if choice == &quot;1&quot;:<\/p>\n<p>            thread = AddBookThread()<\/p>\n<p>            thread.start()<\/p>\n<p>            thread.join()<\/p>\n<p>        elif choice == &quot;2&quot;:<\/p>\n<p>            logic.view_books()<\/p>\n<p>        elif choice == &quot;3&quot;:<\/p>\n<p>            break<\/p>\n<p>        else:<\/p>\n<p>            print(&quot;Invalid choice. Please try again.&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>\u9519\u8bef\u5904\u7406<\/h5>\n<\/p>\n<p><p>\u6dfb\u52a0\u9519\u8bef\u5904\u7406\u6765\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add_book():<\/p>\n<p>    try:<\/p>\n<p>        title = ui.get_user_input(&quot;Enter book title: &quot;)<\/p>\n<p>        if not title:<\/p>\n<p>            raise ValueError(&quot;Title cannot be empty.&quot;)<\/p>\n<p>        author = ui.get_user_input(&quot;Enter book author: &quot;)<\/p>\n<p>        if not author:<\/p>\n<p>            raise ValueError(&quot;Author cannot be empty.&quot;)<\/p>\n<p>        database.add_book({&quot;title&quot;: title, &quot;author&quot;: author})<\/p>\n<p>    except ValueError as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p>def view_books():<\/p>\n<p>    try:<\/p>\n<p>        books = database.get_books()<\/p>\n<p>        if not books:<\/p>\n<p>            raise LookupError(&quot;No books found.&quot;)<\/p>\n<p>        for book in books:<\/p>\n<p>            print(f&quot;Title: {book[&#39;title&#39;]}, Author: {book[&#39;author&#39;]}&quot;)<\/p>\n<p>    except LookupError as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>\u6587\u6863\u5316<\/h5>\n<\/p>\n<p><p>\u4e3a\u4ee3\u7801\u6dfb\u52a0\u6587\u6863\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># logic.py<\/p>\n<p>import database<\/p>\n<p>import ui<\/p>\n<p>def add_book():<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    Adds a new book to the database.<\/p>\n<p>    Prompts the user to enter the book title and author.<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    try:<\/p>\n<p>        title = ui.get_user_input(&quot;Enter book title: &quot;)<\/p>\n<p>        if not title:<\/p>\n<p>            raise ValueError(&quot;Title cannot be empty.&quot;)<\/p>\n<p>        author = ui.get_user_input(&quot;Enter book author: &quot;)<\/p>\n<p>        if not author:<\/p>\n<p>            raise ValueError(&quot;Author cannot be empty.&quot;)<\/p>\n<p>        database.add_book({&quot;title&quot;: title, &quot;author&quot;: author})<\/p>\n<p>    except ValueError as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p>def view_books():<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    Displays all books in the database.<\/p>\n<p>    If no books are found, an error message is displayed.<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    try:<\/p>\n<p>        books = database.get_books()<\/p>\n<p>        if not books:<\/p>\n<p>            raise LookupError(&quot;No books found.&quot;)<\/p>\n<p>        for book in books:<\/p>\n<p>            print(f&quot;Title: {book[&#39;title&#39;]}, Author: {book[&#39;author&#39;]}&quot;)<\/p>\n<p>    except LookupError as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u6a21\u5757\u5316\u8bbe\u8ba1\u3001\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u3001\u591a\u7ebf\u7a0b\u5904\u7406\u3001\u9519\u8bef\u5904\u7406\u548c\u826f\u597d\u7684\u6587\u6863\u5316\uff0c\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u529f\u80fd\u4e30\u5bcc\u4e14\u6613\u4e8e\u7ef4\u62a4\u7684Python\u7a0b\u5e8f\u3002\u6a21\u5757\u5316\u8bbe\u8ba1\u6709\u52a9\u4e8e\u964d\u4f4e\u4ee3\u7801\u7684\u8026\u5408\u5ea6\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u590d\u7528\u6027\uff1b\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u548c\u6613\u4e8e\u6269\u5c55\uff1b\u591a\u7ebf\u7a0b\u5904\u7406\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u548c\u54cd\u5e94\u901f\u5ea6\uff1b\u9519\u8bef\u5904\u7406\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff1b\u826f\u597d\u7684\u6587\u6863\u5316\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u7efc\u5408\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7f16\u5199\u51fa\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u4e14\u9ad8\u6548\u7684Python\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684Python\u5e93\u6765\u5b9e\u73b0\u591a\u529f\u80fd\u7a0b\u5e8f\uff1f<\/strong><br \/>\u5728\u7f16\u5199\u591a\u529f\u80fd\u7a0b\u5e8f\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684Python\u5e93\u81f3\u5173\u91cd\u8981\u3002\u6839\u636e\u7a0b\u5e8f\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982Flask\u6216Django\u7528\u4e8eWeb\u5f00\u53d1\uff0cPandas\u548cNumPy\u7528\u4e8e\u6570\u636e\u5904\u7406\uff0cMatplotlib\u548cSeaborn\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\uff0cTkinter\u6216PyQt\u7528\u4e8e\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u3002\u6bcf\u4e2a\u5e93\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\uff0c\u5177\u4f53\u9009\u62e9\u5e94\u57fa\u4e8e\u9879\u76ee\u9700\u6c42\u548c\u56e2\u961f\u7684\u719f\u6089\u7a0b\u5ea6\u3002<\/p>\n<p><strong>\u5982\u4f55\u7ec4\u7ec7\u4ee3\u7801\u4ee5\u63d0\u9ad8\u591a\u529f\u80fd\u7a0b\u5e8f\u7684\u53ef\u7ef4\u62a4\u6027\uff1f<\/strong><br \/>\u826f\u597d\u7684\u4ee3\u7801\u7ec4\u7ec7\u7ed3\u6784\u662f\u63d0\u9ad8\u7a0b\u5e8f\u53ef\u7ef4\u62a4\u6027\u7684\u5173\u952e\u3002\u53ef\u4ee5\u91c7\u7528\u6a21\u5757\u5316\u8bbe\u8ba1\uff0c\u5c06\u4e0d\u540c\u529f\u80fd\u62c6\u5206\u6210\u591a\u4e2a\u6a21\u5757\u548c\u7c7b\uff0c\u786e\u4fdd\u6bcf\u4e2a\u6a21\u5757\u8d1f\u8d23\u7279\u5b9a\u7684\u529f\u80fd\u3002\u540c\u65f6\uff0c\u4f7f\u7528\u6e05\u6670\u7684\u547d\u540d\u7ea6\u5b9a\u548c\u6ce8\u91ca\uff0c\u5e2e\u52a9\u5176\u4ed6\u5f00\u53d1\u8005\u7406\u89e3\u4ee3\u7801\u903b\u8f91\u3002\u6b64\u5916\uff0c\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u4e5f\u80fd\u4fdd\u8bc1\u4ee3\u7801\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002<\/p>\n<p><strong>\u5728\u7f16\u5199\u591a\u529f\u80fd\u7a0b\u5e8f\u65f6\uff0c\u5982\u4f55\u5904\u7406\u7528\u6237\u8f93\u5165\u548c\u9519\u8bef\uff1f<\/strong><br \/>\u5904\u7406\u7528\u6237\u8f93\u5165\u548c\u9519\u8bef\u662f\u7f16\u5199\u5065\u58ee\u591a\u529f\u80fd\u7a0b\u5e8f\u7684\u91cd\u8981\u73af\u8282\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Python\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\uff08try-except\u8bed\u53e5\uff09\u6765\u6355\u83b7\u53ef\u80fd\u7684\u9519\u8bef\uff0c\u5e76\u63d0\u4f9b\u53cb\u597d\u7684\u9519\u8bef\u63d0\u793a\u3002\u6b64\u5916\uff0c\u8f93\u5165\u9a8c\u8bc1\u4e5f\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\uff0c\u901a\u8fc7\u5b9a\u4e49\u8f93\u5165\u683c\u5f0f\u548c\u8303\u56f4\uff0c\u786e\u4fdd\u7528\u6237\u63d0\u4f9b\u7684\u6570\u636e\u7b26\u5408\u9884\u671f\uff0c\u4ece\u800c\u51cf\u5c11\u7a0b\u5e8f\u5d29\u6e83\u7684\u98ce\u9669\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7f16\u5199\u591a\u529f\u80fdPython\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u6a21\u5757\u5316\u8bbe\u8ba1\u3001\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u3001\u591a\u7ebf\u7a0b\u5904\u7406\u3001\u9519\u8bef\u5904\u7406\u548c\u826f\u597d\u7684\u6587\u6863\u5316\u6765\u5b9e\u73b0\u3002\u8fd9\u4e9b\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1176719,"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\/1176710"}],"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=1176710"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1176710\/revisions"}],"predecessor-version":[{"id":1176722,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1176710\/revisions\/1176722"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1176719"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1176710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1176710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1176710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}