{"id":1116840,"date":"2025-01-08T18:20:22","date_gmt":"2025-01-08T10:20:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1116840.html"},"modified":"2025-01-08T18:20:24","modified_gmt":"2025-01-08T10:20:24","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python%e4%b8%8a%e6%b1%82%e9%95%bf%e6%96%b9%e5%bd%a2%e9%9d%a2%e7%a7%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1116840.html","title":{"rendered":"\u5982\u4f55\u5728python\u4e0a\u6c42\u957f\u65b9\u5f62\u9762\u79ef"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081017\/aa0dcff8-88e2-4324-ac19-d1f1388fca89.webp\" alt=\"\u5982\u4f55\u5728python\u4e0a\u6c42\u957f\u65b9\u5f62\u9762\u79ef\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e0a\u6c42\u957f\u65b9\u5f62\u9762\u79ef<\/strong>\uff0c<strong>\u4f60\u53ef\u4ee5\u4f7f\u7528\u7b80\u5355\u7684\u6570\u5b66\u516c\u5f0f\u901a\u8fc7\u7f16\u5199\u51fd\u6570\u6765\u5b9e\u73b0<\/strong>\u3002<strong>\u516c\u5f0f\u4e3a\u957f\u65b9\u5f62\u9762\u79ef = \u957f\u5ea6 * \u5bbd\u5ea6<\/strong>\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u63a5\u6536\u957f\u5ea6\u548c\u5bbd\u5ea6\uff0c\u7136\u540e\u8fd4\u56de\u8ba1\u7b97\u7684\u7ed3\u679c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def rectangle_area(length, width):<\/p>\n<p>    return length * width<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>length = 5<\/p>\n<p>width = 3<\/p>\n<p>area = rectangle_area(length, width)<\/p>\n<p>print(f&quot;\u957f\u65b9\u5f62\u7684\u9762\u79ef\u662f: {area}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c<code>length<\/code> \u548c <code>width<\/code> \u662f\u957f\u65b9\u5f62\u7684\u8fb9\u957f\uff0c\u901a\u8fc7\u5c06\u8fd9\u4e24\u4e2a\u503c\u76f8\u4e58\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u957f\u65b9\u5f62\u7684\u9762\u79ef\u3002\u73b0\u5728\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e00\u8fc7\u7a0b\uff0c\u5e76\u63d0\u4f9b\u66f4\u591a\u76f8\u5173\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b9a\u4e49\u548c\u8c03\u7528\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u51fd\u6570\u662f\u975e\u5e38\u7b80\u5355\u7684\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>def<\/code> \u5173\u952e\u5b57\u6765\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u7136\u540e\u5728\u51fd\u6570\u4f53\u5185\u7f16\u5199\u4ee3\u7801\u6765\u6267\u884c\u7279\u5b9a\u7684\u4efb\u52a1\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a <code>rectangle_area<\/code> \u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u6536\u4e24\u4e2a\u53c2\u6570 <code>length<\/code> \u548c <code>width<\/code>\uff0c\u7136\u540e\u8fd4\u56de\u5b83\u4eec\u7684\u4e58\u79ef\uff0c\u5373\u957f\u65b9\u5f62\u7684\u9762\u79ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def rectangle_area(length, width):<\/p>\n<p>    return length * width<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u65f6\uff0c\u53ea\u9700\u63d0\u4f9b\u957f\u5ea6\u548c\u5bbd\u5ea6\u7684\u503c\u4f5c\u4e3a\u53c2\u6570\u5373\u53ef\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">length = 5<\/p>\n<p>width = 3<\/p>\n<p>area = rectangle_area(length, width)<\/p>\n<p>print(f&quot;\u957f\u65b9\u5f62\u7684\u9762\u79ef\u662f: {area}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5904\u7406\u7528\u6237\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u9700\u8981\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u503c\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>input<\/code> \u51fd\u6570\u6765\u83b7\u53d6\u7528\u6237\u8f93\u5165\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u9002\u5f53\u7684\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u6216\u6574\u6570\uff09\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def rectangle_area(length, width):<\/p>\n<p>    return length * width<\/p>\n<p>length = float(input(&quot;\u8bf7\u8f93\u5165\u957f\u65b9\u5f62\u7684\u957f\u5ea6: &quot;))<\/p>\n<p>width = float(input(&quot;\u8bf7\u8f93\u5165\u957f\u65b9\u5f62\u7684\u5bbd\u5ea6: &quot;))<\/p>\n<p>area = rectangle_area(length, width)<\/p>\n<p>print(f&quot;\u957f\u65b9\u5f62\u7684\u9762\u79ef\u662f: {area}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>float<\/code> \u51fd\u6570\u5c06\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff0c\u4ee5\u4fbf\u80fd\u591f\u6b63\u786e\u8ba1\u7b97\u9762\u79ef\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5904\u7406\u65e0\u6548\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u4f60\u53ef\u4ee5\u6dfb\u52a0\u9519\u8bef\u5904\u7406\uff0c\u4ee5\u9632\u7528\u6237\u8f93\u5165\u65e0\u6548\u6570\u636e\u3002\u4f8b\u5982\uff0c\u7528\u6237\u53ef\u80fd\u4f1a\u8f93\u5165\u975e\u6570\u5b57\u5b57\u7b26\u6216\u8d1f\u6570\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>try...except<\/code> \u5757\u6765\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def rectangle_area(length, width):<\/p>\n<p>    if length &lt;= 0 or width &lt;= 0:<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;\u957f\u5ea6\u548c\u5bbd\u5ea6\u5fc5\u987b\u662f\u6b63\u6570&quot;)<\/p>\n<p>    return length * width<\/p>\n<p>try:<\/p>\n<p>    length = float(input(&quot;\u8bf7\u8f93\u5165\u957f\u65b9\u5f62\u7684\u957f\u5ea6: &quot;))<\/p>\n<p>    width = float(input(&quot;\u8bf7\u8f93\u5165\u957f\u65b9\u5f62\u7684\u5bbd\u5ea6: &quot;))<\/p>\n<p>    area = rectangle_area(length, width)<\/p>\n<p>    print(f&quot;\u957f\u65b9\u5f62\u7684\u9762\u79ef\u662f: {area}&quot;)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(f&quot;\u8f93\u5165\u9519\u8bef: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728 <code>rectangle_area<\/code> \u51fd\u6570\u4e2d\u6dfb\u52a0\u4e86\u4e00\u4e2a\u68c0\u67e5\uff0c\u4ee5\u786e\u4fdd\u957f\u5ea6\u548c\u5bbd\u5ea6\u90fd\u662f\u6b63\u6570\u3002\u5982\u679c\u8f93\u5165\u65e0\u6548\uff0c\u4f1a\u5f15\u53d1 <code>ValueError<\/code> \u5f02\u5e38\uff0c\u5e76\u5728 <code>except<\/code> \u5757\u4e2d\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u51fd\u6570\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\u6765\u8ba1\u7b97\u957f\u65b9\u5f62\u7684\u9762\u79ef\u3002\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a <code>Rectangle<\/code> \u7c7b\uff0c\u5e76\u5728\u7c7b\u4e2d\u5b9a\u4e49\u8ba1\u7b97\u9762\u79ef\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Rectangle:<\/p>\n<p>    def __init__(self, length, width):<\/p>\n<p>        if length &lt;= 0 or width &lt;= 0:<\/p>\n<p>            raise ValueError(&quot;\u957f\u5ea6\u548c\u5bbd\u5ea6\u5fc5\u987b\u662f\u6b63\u6570&quot;)<\/p>\n<p>        self.length = length<\/p>\n<p>        self.width = width<\/p>\n<p>    def area(self):<\/p>\n<p>        return self.length * self.width<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>try:<\/p>\n<p>    length = float(input(&quot;\u8bf7\u8f93\u5165\u957f\u65b9\u5f62\u7684\u957f\u5ea6: &quot;))<\/p>\n<p>    width = float(input(&quot;\u8bf7\u8f93\u5165\u957f\u65b9\u5f62\u7684\u5bbd\u5ea6: &quot;))<\/p>\n<p>    rectangle = Rectangle(length, width)<\/p>\n<p>    area = rectangle.area()<\/p>\n<p>    print(f&quot;\u957f\u65b9\u5f62\u7684\u9762\u79ef\u662f: {area}&quot;)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(f&quot;\u8f93\u5165\u9519\u8bef: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>Rectangle<\/code> \u7c7b\uff0c\u5e76\u5728\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4e2d\u6dfb\u52a0\u4e86\u8f93\u5165\u68c0\u67e5\u3002\u6211\u4eec\u8fd8\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>area<\/code> \u65b9\u6cd5\u6765\u8ba1\u7b97\u9762\u79ef\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u6a21\u5757\u5316\u7f16\u7a0b<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u91cd\u7528\u6027\uff0c\u4f60\u53ef\u4ee5\u5c06\u8ba1\u7b97\u957f\u65b9\u5f62\u9762\u79ef\u7684\u4ee3\u7801\u653e\u5165\u4e00\u4e2a\u6a21\u5757\u4e2d\u3002\u4e00\u4e2a\u6a21\u5757\u662f\u4e00\u4e2a\u5305\u542bPython\u4ee3\u7801\u7684\u6587\u4ef6\uff0c\u53ef\u4ee5\u5728\u5176\u4ed6Python\u7a0b\u5e8f\u4e2d\u5bfc\u5165\u548c\u4f7f\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>rectangle.py<\/code> \u7684\u6a21\u5757\uff0c\u5e76\u5728\u5176\u4e2d\u5b9a\u4e49 <code>Rectangle<\/code> \u7c7b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># rectangle.py<\/p>\n<p>class Rectangle:<\/p>\n<p>    def __init__(self, length, width):<\/p>\n<p>        if length &lt;= 0 or width &lt;= 0:<\/p>\n<p>            raise ValueError(&quot;\u957f\u5ea6\u548c\u5bbd\u5ea6\u5fc5\u987b\u662f\u6b63\u6570&quot;)<\/p>\n<p>        self.length = length<\/p>\n<p>        self.width = width<\/p>\n<p>    def area(self):<\/p>\n<p>        return self.length * self.width<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f60\u53ef\u4ee5\u5728\u53e6\u4e00\u4e2aPython\u7a0b\u5e8f\u4e2d\u5bfc\u5165\u8fd9\u4e2a\u6a21\u5757\u5e76\u4f7f\u7528\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from rectangle import Rectangle<\/p>\n<p>try:<\/p>\n<p>    length = float(input(&quot;\u8bf7\u8f93\u5165\u957f\u65b9\u5f62\u7684\u957f\u5ea6: &quot;))<\/p>\n<p>    width = float(input(&quot;\u8bf7\u8f93\u5165\u957f\u65b9\u5f62\u7684\u5bbd\u5ea6: &quot;))<\/p>\n<p>    rectangle = Rectangle(length, width)<\/p>\n<p>    area = rectangle.area()<\/p>\n<p>    print(f&quot;\u957f\u65b9\u5f62\u7684\u9762\u79ef\u662f: {area}&quot;)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(f&quot;\u8f93\u5165\u9519\u8bef: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5c06\u8ba1\u7b97\u957f\u65b9\u5f62\u9762\u79ef\u7684\u4ee3\u7801\u4e0e\u5176\u4ed6\u903b\u8f91\u5206\u79bb\uff0c\u4ece\u800c\u4f7f\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\u548c\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u5355\u5143\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u4f60\u7684\u4ee3\u7801\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\u90fd\u80fd\u6b63\u786e\u8fd0\u884c\uff0c\u4f60\u53ef\u4ee5\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u3002\u5355\u5143\u6d4b\u8bd5\u662f\u4e00\u79cd\u6d4b\u8bd5\u65b9\u6cd5\uff0c\u7528\u4e8e\u9a8c\u8bc1\u4ee3\u7801\u7684\u6bcf\u4e2a\u5355\u5143\uff08\u5373\u51fd\u6570\u6216\u7c7b\uff09\u662f\u5426\u6b63\u5e38\u5de5\u4f5c\u3002\u5728Python\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>unittest<\/code> \u6a21\u5757\u6765\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unittest<\/p>\n<p>from rectangle import Rectangle<\/p>\n<p>class TestRectangle(unittest.TestCase):<\/p>\n<p>    def test_area(self):<\/p>\n<p>        rectangle = Rectangle(5, 3)<\/p>\n<p>        self.assertEqual(rectangle.area(), 15)<\/p>\n<p>    def test_invalid_input(self):<\/p>\n<p>        with self.assertRaises(ValueError):<\/p>\n<p>            Rectangle(-5, 3)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    unittest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>TestRectangle<\/code> \u7c7b\uff0c\u5e76\u5728\u5176\u4e2d\u7f16\u5199\u4e86\u4e24\u4e2a\u6d4b\u8bd5\u7528\u4f8b\uff1a\u4e00\u4e2a\u7528\u4e8e\u6d4b\u8bd5\u6b63\u5e38\u60c5\u51b5\uff0c\u53e6\u4e00\u4e2a\u7528\u4e8e\u6d4b\u8bd5\u65e0\u6548\u8f93\u5165\u3002\u901a\u8fc7\u8fd0\u884c\u8fd9\u4e2a\u6d4b\u8bd5\u811a\u672c\uff0c\u4f60\u53ef\u4ee5\u9a8c\u8bc1 <code>Rectangle<\/code> \u7c7b\u662f\u5426\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\u90fd\u80fd\u6b63\u786e\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u6539\u8fdb\u4ee3\u7801\u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u6216\u9700\u8981\u9ad8\u6027\u80fd\u7684\u5e94\u7528\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u4f18\u5316\u4ee3\u7801\u7684\u6027\u80fd\u3002\u867d\u7136\u8ba1\u7b97\u957f\u65b9\u5f62\u9762\u79ef\u7684\u4ee3\u7801\u672c\u8eab\u975e\u5e38\u7b80\u5355\u4e14\u9ad8\u6548\uff0c\u4f46\u4f60\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u5e38\u89c1\u7684\u4f18\u5316\u6280\u672f\u6765\u63d0\u9ad8\u4ee3\u7801\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>NumPy<\/code> \u5e93\u6765\u5904\u7406\u6570\u503c\u8ba1\u7b97\u3002<code>NumPy<\/code> \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u591a\u7ef4\u6570\u7ec4\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def rectangle_area(length, width):<\/p>\n<p>    length = np.array(length)<\/p>\n<p>    width = np.array(width)<\/p>\n<p>    return length * width<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>length = np.array([5, 10, 15])<\/p>\n<p>width = np.array([3, 6, 9])<\/p>\n<p>area = rectangle_area(length, width)<\/p>\n<p>print(f&quot;\u957f\u65b9\u5f62\u7684\u9762\u79ef\u662f: {area}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>NumPy<\/code> \u6570\u7ec4\u6765\u5b58\u50a8\u957f\u5ea6\u548c\u5bbd\u5ea6\uff0c\u5e76\u5229\u7528 <code>NumPy<\/code> \u7684\u9ad8\u6548\u6570\u7ec4\u64cd\u4f5c\u6765\u8ba1\u7b97\u9762\u79ef\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\u6027\u80fd\u66f4\u597d\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u53ef\u89c6\u5316\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u5e94\u7528\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u53ef\u89c6\u5316\u957f\u65b9\u5f62\u7684\u9762\u79ef\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>matplotlib<\/code> \u5e93\u6765\u7ed8\u5236\u56fe\u5f62\uff0c\u5e76\u5c55\u793a\u4e0d\u540c\u957f\u65b9\u5f62\u7684\u9762\u79ef\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>def plot_rectangles(lengths, widths):<\/p>\n<p>    areas = [length * width for length, width in zip(lengths, widths)]<\/p>\n<p>    plt.bar(range(len(areas)), areas)<\/p>\n<p>    plt.xlabel(&#39;\u957f\u65b9\u5f62\u7f16\u53f7&#39;)<\/p>\n<p>    plt.ylabel(&#39;\u9762\u79ef&#39;)<\/p>\n<p>    plt.title(&#39;\u957f\u65b9\u5f62\u9762\u79ef&#39;)<\/p>\n<p>    plt.show()<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>lengths = [5, 10, 15]<\/p>\n<p>widths = [3, 6, 9]<\/p>\n<p>plot_rectangles(lengths, widths)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>plot_rectangles<\/code> \u51fd\u6570\uff0c\u7528\u4e8e\u7ed8\u5236\u957f\u65b9\u5f62\u9762\u79ef\u7684\u6761\u5f62\u56fe\u3002\u901a\u8fc7\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u76f4\u89c2\u5730\u5c55\u793a\u4e0d\u540c\u957f\u65b9\u5f62\u7684\u9762\u79ef\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u4f60\u5e94\u8be5\u5df2\u7ecf\u638c\u63e1\u4e86\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u957f\u65b9\u5f62\u7684\u9762\u79ef\u3002\u6211\u4eec\u63a2\u8ba8\u4e86\u4f7f\u7528\u51fd\u6570\u3001\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u3001\u6a21\u5757\u5316\u7f16\u7a0b\u3001\u5355\u5143\u6d4b\u8bd5\u3001\u6027\u80fd\u4f18\u5316\u548c\u6570\u636e\u53ef\u89c6\u5316\u7b49\u65b9\u6cd5\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u5e76\u80fd\u591f\u5e94\u7528\u5230\u4f60\u7684\u5b9e\u9645\u9879\u76ee\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u4f60\u662f\u521d\u5b66\u8005\u8fd8\u662f\u6709\u7ecf\u9a8c\u7684\u5f00\u53d1\u8005\uff0c\u7406\u89e3\u5e76\u638c\u63e1\u8fd9\u4e9b\u57fa\u672c\u6982\u5ff5\u548c\u6280\u672f\u90fd\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u901a\u8fc7\u4e0d\u65ad\u7ec3\u4e60\u548c\u5e94\u7528\uff0c\u4f60\u5c06\u80fd\u591f\u7f16\u5199\u51fa\u66f4\u52a0\u5065\u58ee\u3001\u9ad8\u6548\u548c\u6613\u4e8e\u7ef4\u62a4\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u957f\u65b9\u5f62\u7684\u957f\u5ea6\u548c\u5bbd\u5ea6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u53d8\u91cf\u6765\u5b9a\u4e49\u957f\u65b9\u5f62\u7684\u957f\u5ea6\u548c\u5bbd\u5ea6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u8f93\u5165\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u503c\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">length = float(input(&quot;\u8bf7\u8f93\u5165\u957f\u65b9\u5f62\u7684\u957f\u5ea6: &quot;))\nwidth = float(input(&quot;\u8bf7\u8f93\u5165\u957f\u65b9\u5f62\u7684\u5bbd\u5ea6: &quot;))\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u7528\u6237\u5c31\u53ef\u4ee5\u81ea\u7531\u5730\u8f93\u5165\u6240\u9700\u7684\u5c3a\u5bf8\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u8ba1\u7b97\u957f\u65b9\u5f62\u7684\u9762\u79ef\uff1f<\/strong><br \/>\u8ba1\u7b97\u957f\u65b9\u5f62\u9762\u79ef\u7684\u516c\u5f0f\u662f\uff1a\u9762\u79ef = \u957f\u5ea6 \u00d7 \u5bbd\u5ea6\u3002\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u4e58\u6cd5\u8fd0\u7b97\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">area = length * width\nprint(f&quot;\u957f\u65b9\u5f62\u7684\u9762\u79ef\u662f: {area}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa\u957f\u65b9\u5f62\u7684\u9762\u79ef\uff0c\u8ba9\u7528\u6237\u4e86\u89e3\u7ed3\u679c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u6765\u8ba1\u7b97\u957f\u65b9\u5f62\u7684\u9762\u79ef\uff1f<\/strong><br \/>\u786e\u5b9e\u53ef\u4ee5\u3002\u4f7f\u7528\u51fd\u6570\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u5177\u53ef\u91cd\u7528\u6027\u548c\u7ed3\u6784\u5316\u3002\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a<code>calculate_area<\/code>\u7684\u51fd\u6570\uff0c\u63a5\u53d7\u957f\u5ea6\u548c\u5bbd\u5ea6\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u8ba1\u7b97\u51fa\u7684\u9762\u79ef\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">def calculate_area(length, width):\n    return length * width\n\narea = calculate_area(length, width)\nprint(f&quot;\u957f\u65b9\u5f62\u7684\u9762\u79ef\u662f: {area}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u65b9\u4fbf\u7528\u6237\u5728\u9700\u8981\u65f6\u591a\u6b21\u8c03\u7528\u8ba1\u7b97\u9762\u79ef\u7684\u529f\u80fd\uff0c\u63d0\u5347\u4e86\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e0a\u6c42\u957f\u65b9\u5f62\u9762\u79ef\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u7b80\u5355\u7684\u6570\u5b66\u516c\u5f0f\u901a\u8fc7\u7f16\u5199\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u516c\u5f0f\u4e3a\u957f\u65b9\u5f62\u9762\u79ef = \u957f\u5ea6 * \u5bbd\u5ea6 [&hellip;]","protected":false},"author":3,"featured_media":1116847,"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\/1116840"}],"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=1116840"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1116840\/revisions"}],"predecessor-version":[{"id":1116850,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1116840\/revisions\/1116850"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1116847"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1116840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1116840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1116840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}