{"id":1094575,"date":"2025-01-08T14:42:20","date_gmt":"2025-01-08T06:42:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1094575.html"},"modified":"2025-01-08T14:42:23","modified_gmt":"2025-01-08T06:42:23","slug":"python%e5%86%99%e7%a8%8b%e5%ba%8f%e6%97%b6%e5%a6%82%e4%bd%95%e6%9b%b4%e6%94%b9%e9%94%99%e8%af%af-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1094575.html","title":{"rendered":"python\u5199\u7a0b\u5e8f\u65f6\u5982\u4f55\u66f4\u6539\u9519\u8bef"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210616\/9cc197b1-e68e-48df-9bb1-6058ffbe76c5.webp\" alt=\"python\u5199\u7a0b\u5e8f\u65f6\u5982\u4f55\u66f4\u6539\u9519\u8bef\" \/><\/p>\n<p><p> <strong>\u5728Python\u7f16\u7a0b\u4e2d\u66f4\u6539\u9519\u8bef\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u7406\u89e3\u9519\u8bef\u4fe1\u606f\u3001\u8c03\u8bd5\u4ee3\u7801\u3001\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u3001\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b\u3001\u5b66\u4e60\u5e38\u89c1\u9519\u8bef\u3001\u4f7f\u7528\u5f00\u53d1\u5de5\u5177\u3002<\/strong>\u4ee5\u4e0b\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u4e2a\u65b9\u6cd5\uff1a<strong>\u7406\u89e3\u9519\u8bef\u4fe1\u606f<\/strong>\u3002\u5f53Python\u7a0b\u5e8f\u51fa\u9519\u65f6\uff0c\u89e3\u91ca\u5668\u4f1a\u63d0\u4f9b\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u5305\u62ec\u9519\u8bef\u7c7b\u578b\u3001\u9519\u8bef\u539f\u56e0\u548c\u51fa\u9519\u7684\u5177\u4f53\u4f4d\u7f6e\u3002\u7406\u89e3\u5e76\u5206\u6790\u8fd9\u4e9b\u9519\u8bef\u4fe1\u606f\u662f\u89e3\u51b3\u95ee\u9898\u7684\u7b2c\u4e00\u6b65\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u7406\u89e3\u9519\u8bef\u4fe1\u606f<\/p>\n<\/p>\n<p><p>Python\u5728\u9047\u5230\u9519\u8bef\u65f6\u4f1a\u629b\u51fa\u5f02\u5e38\uff0c\u751f\u6210\u9519\u8bef\u4fe1\u606f\uff08traceback\uff09\u3002\u8fd9\u4e2a\u4fe1\u606f\u5305\u542b\u4e86\u9519\u8bef\u7c7b\u578b\u3001\u9519\u8bef\u539f\u56e0\u4ee5\u53ca\u4ee3\u7801\u4e2d\u51fa\u9519\u7684\u4f4d\u7f6e\u3002\u7406\u89e3\u8fd9\u4e9b\u4fe1\u606f\u53ef\u4ee5\u5e2e\u52a9\u5feb\u901f\u5b9a\u4f4d\u548c\u4fee\u590d\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u5982\u679c\u9047\u5230<code>SyntaxError<\/code>\uff0c\u901a\u5e38\u662f\u8bed\u6cd5\u95ee\u9898\uff1b\u800c<code>IndexError<\/code>\u5219\u53ef\u80fd\u662f\u8bbf\u95ee\u5217\u8868\u65f6\u4f7f\u7528\u4e86\u65e0\u6548\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><p><strong>\u5e38\u89c1\u9519\u8bef\u7c7b\u578b<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>SyntaxError<\/strong>\uff1a\u8fd9\u662f\u6700\u5e38\u89c1\u7684\u9519\u8bef\u4e4b\u4e00\uff0c\u901a\u5e38\u662f\u7531\u4e8e\u4ee3\u7801\u8bed\u6cd5\u4e0d\u6b63\u786e\u5f15\u8d77\u7684\u3002\u4f8b\u5982\uff0c\u6f0f\u6389\u4e86\u5192\u53f7\u3001\u62ec\u53f7\u6ca1\u6709\u95ed\u5408\u3001\u7f29\u8fdb\u9519\u8bef\u7b49\u3002\u8fd9\u7c7b\u9519\u8bef\u5728\u4ee3\u7801\u7f16\u5199\u9636\u6bb5\u5c31\u4f1a\u88ab\u53d1\u73b0\uff0c\u65e0\u6cd5\u901a\u8fc7\u89e3\u91ca\u5668\u7684\u8bed\u6cd5\u68c0\u67e5\u3002<\/li>\n<li><strong>IndentationError<\/strong>\uff1aPython\u5bf9\u4ee3\u7801\u7684\u7f29\u8fdb\u975e\u5e38\u654f\u611f\uff0c\u7f29\u8fdb\u4e0d\u6b63\u786e\u4f1a\u5f15\u53d1\u6b64\u7c7b\u9519\u8bef\u3002\u901a\u5e38\u662f\u56e0\u4e3a\u4e0d\u4e00\u81f4\u7684\u7f29\u8fdb\u98ce\u683c\uff08\u6bd4\u5982\u6df7\u7528\u4e86\u7a7a\u683c\u548c\u5236\u8868\u7b26\uff09\u6216\u8005\u7f29\u8fdb\u7ea7\u522b\u4e0d\u6b63\u786e\u3002<\/li>\n<li><strong>NameError<\/strong>\uff1a\u5f53\u4ee3\u7801\u4e2d\u4f7f\u7528\u4e86\u672a\u5b9a\u4e49\u7684\u53d8\u91cf\u6216\u51fd\u6570\u65f6\uff0c\u4f1a\u5f15\u53d1\u6b64\u7c7b\u9519\u8bef\u3002\u9700\u8981\u68c0\u67e5\u53d8\u91cf\u6216\u51fd\u6570\u662f\u5426\u6b63\u786e\u5b9a\u4e49\u5e76\u5728\u4f7f\u7528\u524d\u521d\u59cb\u5316\u3002<\/li>\n<li><strong>TypeError<\/strong>\uff1a\u5f53\u64cd\u4f5c\u6216\u51fd\u6570\u63a5\u6536\u5230\u4e0e\u9884\u671f\u7c7b\u578b\u4e0d\u7b26\u7684\u53c2\u6570\u65f6\uff0c\u4f1a\u5f15\u53d1\u6b64\u7c7b\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u4e32\u4e0e\u6574\u6570\u76f8\u52a0\u3002<\/li>\n<li><strong>IndexError<\/strong>\uff1a\u8bbf\u95ee\u5217\u8868\u3001\u5143\u7ec4\u7b49\u5e8f\u5217\u65f6\uff0c\u4f7f\u7528\u4e86\u65e0\u6548\u7684\u7d22\u5f15\u503c\uff08\u8d85\u51fa\u8303\u56f4\uff09\u4f1a\u5f15\u53d1\u6b64\u7c7b\u9519\u8bef\u3002<\/li>\n<li><strong>KeyError<\/strong>\uff1a\u5728\u4f7f\u7528\u5b57\u5178\u65f6\uff0c\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u4f1a\u5f15\u53d1\u6b64\u7c7b\u9519\u8bef\u3002\u53ef\u4ee5\u901a\u8fc7<code>in<\/code>\u64cd\u4f5c\u7b26\u6216\u8005<code>get<\/code>\u65b9\u6cd5\u6765\u9884\u9632\u3002<\/li>\n<li><strong>ValueError<\/strong>\uff1a\u5f53\u51fd\u6570\u63a5\u6536\u5230\u53c2\u6570\u7c7b\u578b\u6b63\u786e\u4f46\u503c\u4e0d\u5408\u9002\u65f6\uff0c\u4f1a\u5f15\u53d1\u6b64\u7c7b\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u65f6\u63d0\u4f9b\u4e86\u975e\u6570\u5b57\u5b57\u7b26\u4e32\u3002<\/li>\n<li><strong>AttributeError<\/strong>\uff1a\u5f53\u5c1d\u8bd5\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u5bf9\u8c61\u5c5e\u6027\u65f6\uff0c\u4f1a\u5f15\u53d1\u6b64\u7c7b\u9519\u8bef\u3002\u9700\u8981\u68c0\u67e5\u5bf9\u8c61\u662f\u5426\u6b63\u786e\u521d\u59cb\u5316\uff0c\u5c5e\u6027\u540d\u662f\u5426\u6b63\u786e\u3002<\/li>\n<li><strong>ImportError<\/strong>\uff1a\u5f53\u6a21\u5757\u5bfc\u5165\u5931\u8d25\u65f6\uff0c\u4f1a\u5f15\u53d1\u6b64\u7c7b\u9519\u8bef\u3002\u9700\u8981\u68c0\u67e5\u6a21\u5757\u662f\u5426\u5b58\u5728\u5e76\u4e14\u8def\u5f84\u6b63\u786e\u3002<\/li>\n<\/ol>\n<p><p>\u7406\u89e3\u8fd9\u4e9b\u9519\u8bef\u7c7b\u578b\uff0c\u5e76\u7ed3\u5408\u9519\u8bef\u4fe1\u606f\u4e2d\u7684\u5177\u4f53\u63d0\u793a\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5feb\u901f\u5b9a\u4f4d\u95ee\u9898\u6240\u5728\uff0c\u4ece\u800c\u8fdb\u884c\u4fee\u590d\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u8c03\u8bd5\u4ee3\u7801<\/p>\n<\/p>\n<p><p>\u8c03\u8bd5\u4ee3\u7801\u662f\u53d1\u73b0\u548c\u4fee\u590d\u9519\u8bef\u7684\u6709\u6548\u65b9\u6cd5\u4e4b\u4e00\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u8c03\u8bd5\u5de5\u5177\u548c\u6280\u672f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u9010\u6b65\u68c0\u67e5\u4ee3\u7801\u6267\u884c\u8fc7\u7a0b\uff0c\u5b9a\u4f4d\u95ee\u9898\u6839\u6e90\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528print\u8bed\u53e5<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4ee3\u7801\u4e2d\u63d2\u5165<code>print<\/code>\u8bed\u53e5\uff0c\u53ef\u4ee5\u8f93\u51fa\u53d8\u91cf\u7684\u503c\u548c\u7a0b\u5e8f\u6267\u884c\u7684\u72b6\u6001\u3002\u8fd9\u662f\u4e00\u79cd\u7b80\u5355\u4f46\u6709\u6548\u7684\u8c03\u8bd5\u65b9\u6cd5\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u4ee3\u7801\u3002\u901a\u8fc7\u9010\u6b65\u63d2\u5165<code>print<\/code>\u8bed\u53e5\uff0c\u53ef\u4ee5\u8ddf\u8e2a\u7a0b\u5e8f\u6267\u884c\u6d41\u7a0b\uff0c\u627e\u5230\u51fa\u9519\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_sum(a, b):<\/p>\n<p>    print(f&quot;Calculating the sum of {a} and {b}&quot;)<\/p>\n<p>    result = a + b<\/p>\n<p>    print(f&quot;Result: {result}&quot;)<\/p>\n<p>    return result<\/p>\n<p>calculate_sum(5, &#39;10&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528pdb\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>Python\u7684<code>pdb<\/code>\u6a21\u5757\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u8c03\u8bd5\u5de5\u5177\uff0c\u53ef\u4ee5\u9010\u6b65\u6267\u884c\u4ee3\u7801\u3001\u8bbe\u7f6e\u65ad\u70b9\u3001\u68c0\u67e5\u53d8\u91cf\u503c\u7b49\u3002\u4f7f\u7528<code>pdb<\/code>\u53ef\u4ee5\u66f4\u7ec6\u81f4\u5730\u8c03\u8bd5\u590d\u6742\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pdb<\/p>\n<p>def calculate_sum(a, b):<\/p>\n<p>    pdb.set_trace()  # \u8bbe\u7f6e\u65ad\u70b9<\/p>\n<p>    result = a + b<\/p>\n<p>    return result<\/p>\n<p>calculate_sum(5, 10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6267\u884c\u4ee3\u7801\u540e\uff0c\u7a0b\u5e8f\u4f1a\u5728\u65ad\u70b9\u5904\u6682\u505c\uff0c\u8fdb\u5165\u4ea4\u4e92\u5f0f\u8c03\u8bd5\u6a21\u5f0f\u3002\u53ef\u4ee5\u4f7f\u7528<code>p<\/code>\u547d\u4ee4\u67e5\u770b\u53d8\u91cf\u503c\uff0c\u4f7f\u7528<code>n<\/code>\u547d\u4ee4\u9010\u6b65\u6267\u884c\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528IDE\u8c03\u8bd5\u5de5\u5177<\/strong><\/p>\n<\/p>\n<p><p>\u8bb8\u591a\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u8c03\u8bd5\u5de5\u5177\uff0c\u5982PyCharm\u3001Visual Studio Code\u7b49\u3002\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bbe\u7f6e\u65ad\u70b9\u3001\u9010\u6b65\u6267\u884c\u4ee3\u7801\u3001\u67e5\u770b\u53d8\u91cf\u503c\u7b49\uff0c\u6781\u5927\u63d0\u9ad8\u8c03\u8bd5\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5f02\u5e38\u5904\u7406<\/p>\n<\/p>\n<p><p>\u5f02\u5e38\u5904\u7406\u662fPython\u63d0\u4f9b\u7684\u4e00\u79cd\u673a\u5236\uff0c\u7528\u4e8e\u6355\u83b7\u548c\u5904\u7406\u7a0b\u5e8f\u8fd0\u884c\u65f6\u7684\u9519\u8bef\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u5f02\u5e38\u5904\u7406\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u5bb9\u9519\u6027\uff0c\u907f\u514d\u7a0b\u5e8f\u56e0\u9519\u8bef\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>try<\/code>\u3001<code>except<\/code>\u8bed\u53e5\u53ef\u4ee5\u6355\u83b7\u548c\u5904\u7406\u7279\u5b9a\u7c7b\u578b\u7684\u5f02\u5e38\u3002\u53ef\u4ee5\u5728<code>except<\/code>\u5757\u4e2d\u7f16\u5199\u5904\u7406\u4ee3\u7801\uff0c\u6216\u8005\u5728<code>finally<\/code>\u5757\u4e2d\u7f16\u5199\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\u90fd\u8981\u6267\u884c\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    result = 10 \/ 0<\/p>\n<p>except ZeroDivisionError:<\/p>\n<p>    print(&quot;Error: Division by zero&quot;)<\/p>\n<p>finally:<\/p>\n<p>    print(&quot;Execution complete&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u6355\u83b7\u591a\u79cd\u5f02\u5e38<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a<code>except<\/code>\u5757\uff0c\u5206\u522b\u6355\u83b7\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    result = int(&#39;abc&#39;)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;Error: Invalid value&quot;)<\/p>\n<p>except TypeError:<\/p>\n<p>    print(&quot;Error: Type mismatch&quot;)<\/p>\n<p>finally:<\/p>\n<p>    print(&quot;Execution complete&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528else\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u5728<code>try<\/code>\u5757\u4e2d\u6ca1\u6709\u53d1\u751f\u5f02\u5e38\u65f6\uff0c\u53ef\u4ee5\u6267\u884c<code>else<\/code>\u5757\u4e2d\u7684\u4ee3\u7801\u3002\u8fd9\u6837\u53ef\u4ee5\u5c06\u6b63\u5e38\u6267\u884c\u8def\u5f84\u4e0e\u5f02\u5e38\u5904\u7406\u5206\u5f00\uff0c\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    result = int(&#39;123&#39;)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;Error: Invalid value&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;Result: {result}&quot;)<\/p>\n<p>finally:<\/p>\n<p>    print(&quot;Execution complete&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u5f02\u5e38\u5904\u7406\uff0c\u53ef\u4ee5\u6355\u83b7\u5e76\u5904\u7406\u9884\u671f\u7684\u9519\u8bef\u60c5\u51b5\uff0c\u907f\u514d\u7a0b\u5e8f\u56e0\u5f02\u5e38\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b<\/p>\n<\/p>\n<p><p>\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b\u662f\u53d1\u73b0\u548c\u4fee\u590d\u9519\u8bef\u7684\u91cd\u8981\u624b\u6bb5\u3002\u901a\u8fc7\u7f16\u5199\u5168\u9762\u7684\u6d4b\u8bd5\u7528\u4f8b\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u53d1\u5e03\u524d\u53d1\u73b0\u6f5c\u5728\u7684\u95ee\u9898\uff0c\u5e76\u786e\u4fdd\u4ee3\u7801\u7684\u6b63\u786e\u6027\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528unittest\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>Python\u7684<code>unittest<\/code>\u6a21\u5757\u662f\u6807\u51c6\u7684\u6d4b\u8bd5\u6846\u67b6\uff0c\u53ef\u4ee5\u7528\u6765\u7f16\u5199\u548c\u8fd0\u884c\u6d4b\u8bd5\u7528\u4f8b\u3002\u901a\u8fc7\u5b9a\u4e49\u6d4b\u8bd5\u7c7b\u548c\u6d4b\u8bd5\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6d4b\u8bd5\u4ee3\u7801\u7684\u5404\u4e2a\u65b9\u9762\uff0c\u5e76\u9a8c\u8bc1\u5176\u884c\u4e3a\u662f\u5426\u7b26\u5408\u9884\u671f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unittest<\/p>\n<p>def calculate_sum(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>class TestCalculateSum(unittest.TestCase):<\/p>\n<p>    def test_add_integers(self):<\/p>\n<p>        self.assertEqual(calculate_sum(1, 2), 3)<\/p>\n<p>    def test_add_floats(self):<\/p>\n<p>        self.assertEqual(calculate_sum(1.5, 2.5), 4.0)<\/p>\n<p>    def test_add_strings(self):<\/p>\n<p>        self.assertEqual(calculate_sum(&#39;Hello&#39;, &#39; World&#39;), &#39;Hello World&#39;)<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    unittest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u6d4b\u8bd5\u7528\u4f8b\u65f6\uff0c<code>unittest<\/code>\u4f1a\u81ea\u52a8\u6267\u884c\u6240\u6709\u5b9a\u4e49\u7684\u6d4b\u8bd5\u65b9\u6cd5\uff0c\u5e76\u62a5\u544a\u6d4b\u8bd5\u7ed3\u679c\u3002\u5982\u679c\u67d0\u4e2a\u6d4b\u8bd5\u65b9\u6cd5\u5931\u8d25\uff0c\u53ef\u4ee5\u6839\u636e\u9519\u8bef\u4fe1\u606f\u5b9a\u4f4d\u95ee\u9898\u5e76\u8fdb\u884c\u4fee\u590d\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528pytest\u6846\u67b6<\/strong><\/p>\n<\/p>\n<p><p><code>pytest<\/code>\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u7684\u6d4b\u8bd5\u6846\u67b6\uff0c\u652f\u6301\u66f4\u7b80\u6d01\u7684\u6d4b\u8bd5\u7528\u4f8b\u7f16\u5199\u65b9\u5f0f\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u63d2\u4ef6\u751f\u6001\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_sum(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>def test_add_integers():<\/p>\n<p>    assert calculate_sum(1, 2) == 3<\/p>\n<p>def test_add_floats():<\/p>\n<p>    assert calculate_sum(1.5, 2.5) == 4.0<\/p>\n<p>def test_add_strings():<\/p>\n<p>    assert calculate_sum(&#39;Hello&#39;, &#39; World&#39;) == &#39;Hello World&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c<code>pytest<\/code>\u547d\u4ee4\u4f1a\u81ea\u52a8\u53d1\u73b0\u5e76\u6267\u884c\u6240\u6709\u6d4b\u8bd5\u7528\u4f8b\uff0c\u5e76\u751f\u6210\u8be6\u7ec6\u7684\u6d4b\u8bd5\u62a5\u544a\u3002\u901a\u8fc7\u7f16\u5199\u548c\u8fd0\u884c\u6d4b\u8bd5\u7528\u4f8b\uff0c\u53ef\u4ee5\u786e\u4fdd\u4ee3\u7801\u5728\u5404\u79cd\u573a\u666f\u4e0b\u7684\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5b66\u4e60\u5e38\u89c1\u9519\u8bef<\/p>\n<\/p>\n<p><p>\u5b66\u4e60\u548c\u7406\u89e3\u5e38\u89c1\u7684\u9519\u8bef\u7c7b\u578b\u548c\u89e3\u51b3\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u89e3\u51b3\u95ee\u9898\u7684\u6548\u7387\u3002\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u907f\u514d\u5e38\u89c1\u7684\u9677\u9631\u548c\u9519\u8bef\uff0c\u80fd\u591f\u51cf\u5c11\u8c03\u8bd5\u548c\u4fee\u590d\u7684\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e86\u89e3\u5e38\u89c1\u9519\u8bef\u6a21\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u8bb8\u591a\u7f16\u7a0b\u9519\u8bef\u90fd\u6709\u7279\u5b9a\u7684\u6a21\u5f0f\uff0c\u5982\u7a7a\u503c\u5f15\u7528\u3001\u8d8a\u754c\u8bbf\u95ee\u3001\u7c7b\u578b\u4e0d\u5339\u914d\u7b49\u3002\u4e86\u89e3\u8fd9\u4e9b\u9519\u8bef\u6a21\u5f0f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5feb\u901f\u5b9a\u4f4d\u95ee\u9898\u5e76\u627e\u5230\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b66\u4e60\u4f18\u79c0\u4ee3\u7801<\/strong><\/p>\n<\/p>\n<p><p>\u9605\u8bfb\u548c\u5b66\u4e60\u4f18\u79c0\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7f16\u5199\u9ad8\u8d28\u91cf\u4ee3\u7801\u7684\u80fd\u529b\u3002\u901a\u8fc7\u5206\u6790\u548c\u501f\u9274\u4ed6\u4eba\u7684\u4ee3\u7801\u98ce\u683c\u3001\u8bbe\u8ba1\u6a21\u5f0f\u548c\u9519\u8bef\u5904\u7406\u65b9\u6cd5\uff0c\u53ef\u4ee5\u907f\u514d\u5e38\u89c1\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p><strong>\u53c2\u4e0e<a href=\"https:\/\/docs.pingcode.com\/code-review\" target=\"_blank\">\u4ee3\u7801\u5ba1\u67e5<\/a><\/strong><\/p>\n<\/p>\n<p><p>\u4ee3\u7801\u5ba1\u67e5\u662f\u53d1\u73b0\u548c\u4fee\u590d\u9519\u8bef\u7684\u91cd\u8981\u624b\u6bb5\u3002\u901a\u8fc7\u53c2\u4e0e\u4ee3\u7801\u5ba1\u67e5\uff0c\u53ef\u4ee5\u4ece\u4ed6\u4eba\u7684\u53cd\u9988\u4e2d\u5b66\u4e60\uff0c\u5e76\u63d0\u9ad8\u81ea\u5df1\u7684\u4ee3\u7801\u8d28\u91cf\u3002\u540c\u65f6\uff0c\u901a\u8fc7\u5ba1\u67e5\u4ed6\u4eba\u7684\u4ee3\u7801\uff0c\u4e5f\u53ef\u4ee5\u79ef\u7d2f\u89e3\u51b3\u95ee\u9898\u7684\u7ecf\u9a8c\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528\u5f00\u53d1\u5de5\u5177<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5408\u9002\u7684\u5f00\u53d1\u5de5\u5177\u53ef\u4ee5\u63d0\u9ad8\u7f16\u5199\u548c\u8c03\u8bd5\u4ee3\u7801\u7684\u6548\u7387\uff0c\u51cf\u5c11\u9519\u8bef\u7684\u53d1\u751f\u3002\u8fd9\u4e9b\u5de5\u5177\u5305\u62ec\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u3001\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177\u3001\u7248\u672c\u63a7\u5236\u7cfb\u7edf\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09<\/strong><\/p>\n<\/p>\n<p><p>IDE\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u4ee3\u7801\u7f16\u8f91\u548c\u8c03\u8bd5\u529f\u80fd\uff0c\u5982\u8bed\u6cd5\u9ad8\u4eae\u3001\u81ea\u52a8\u8865\u5168\u3001\u4ee3\u7801\u91cd\u6784\u3001\u65ad\u70b9\u8c03\u8bd5\u7b49\u3002\u4f7f\u7528IDE\u53ef\u4ee5\u63d0\u9ad8\u7f16\u5199\u548c\u8c03\u8bd5\u4ee3\u7801\u7684\u6548\u7387\uff0c\u51cf\u5c11\u9519\u8bef\u7684\u53d1\u751f\u3002<\/p>\n<\/p>\n<p><p><strong>\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177<\/strong><\/p>\n<\/p>\n<p><p>\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177\u53ef\u4ee5\u5728\u4ee3\u7801\u7f16\u5199\u9636\u6bb5\u68c0\u67e5\u4ee3\u7801\u7684\u6f5c\u5728\u95ee\u9898\uff0c\u5982\u8bed\u6cd5\u9519\u8bef\u3001\u7c7b\u578b\u4e0d\u5339\u914d\u3001\u4ee3\u7801\u98ce\u683c\u7b49\u3002\u901a\u8fc7\u4f7f\u7528\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177\uff0c\u53ef\u4ee5\u5728\u65e9\u671f\u53d1\u73b0\u5e76\u4fee\u590d\u9519\u8bef\uff0c\u63d0\u9ad8\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528flake8\u8fdb\u884c\u9759\u6001\u4ee3\u7801\u5206\u6790<\/p>\n<p>$ flake8 my_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u7248\u672c\u63a7\u5236\u7cfb\u7edf<\/strong><\/p>\n<\/p>\n<p><p>\u7248\u672c\u63a7\u5236\u7cfb\u7edf\uff08\u5982Git\uff09\u53ef\u4ee5\u8ddf\u8e2a\u4ee3\u7801\u7684\u4fee\u6539\u5386\u53f2\uff0c\u65b9\u4fbf\u56de\u6eda\u5230\u4e4b\u524d\u7684\u7248\u672c\u3002\u901a\u8fc7\u4f7f\u7528\u7248\u672c\u63a7\u5236\u7cfb\u7edf\uff0c\u53ef\u4ee5\u5728\u53d1\u751f\u9519\u8bef\u65f6\u5feb\u901f\u6062\u590d\uff0c\u5e76\u8fdb\u884c\u534f\u540c\u5f00\u53d1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\"># \u4f7f\u7528git\u8fdb\u884c\u7248\u672c\u63a7\u5236<\/p>\n<p>$ git init<\/p>\n<p>$ git add my_script.py<\/p>\n<p>$ git commit -m &quot;Initial commit&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u5408\u9002\u7684\u5f00\u53d1\u5de5\u5177\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7f16\u5199\u548c\u8c03\u8bd5\u4ee3\u7801\u7684\u6548\u7387\uff0c\u51cf\u5c11\u9519\u8bef\u7684\u53d1\u751f\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u7f16\u7a0b\u4e2d\uff0c\u66f4\u6539\u9519\u8bef\u7684\u65b9\u6cd5\u5305\u62ec\u7406\u89e3\u9519\u8bef\u4fe1\u606f\u3001\u8c03\u8bd5\u4ee3\u7801\u3001\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u3001\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b\u3001\u5b66\u4e60\u5e38\u89c1\u9519\u8bef\u548c\u4f7f\u7528\u5f00\u53d1\u5de5\u5177\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u89e3\u51b3\u95ee\u9898\u7684\u6548\u7387\uff0c\u7f16\u5199\u9ad8\u8d28\u91cf\u3001\u5065\u58ee\u7684\u4ee3\u7801\u3002\u7406\u89e3\u9519\u8bef\u4fe1\u606f\u662f\u89e3\u51b3\u95ee\u9898\u7684\u7b2c\u4e00\u6b65\uff0c\u800c\u8c03\u8bd5\u4ee3\u7801\u3001\u5f02\u5e38\u5904\u7406\u548c\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b\u662f\u53d1\u73b0\u548c\u4fee\u590d\u9519\u8bef\u7684\u91cd\u8981\u624b\u6bb5\u3002\u5b66\u4e60\u5e38\u89c1\u9519\u8bef\u548c\u4f7f\u7528\u5408\u9002\u7684\u5f00\u53d1\u5de5\u5177\uff0c\u53ef\u4ee5\u51cf\u5c11\u9519\u8bef\u7684\u53d1\u751f\uff0c\u63d0\u9ad8\u7f16\u7a0b\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8c03\u8bd5\u548c\u67e5\u627e\u9519\u8bef\uff1f<\/strong><br \/>\u8c03\u8bd5Python\u7a0b\u5e8f\u662f\u89e3\u51b3\u9519\u8bef\u7684\u5173\u952e\u6b65\u9aa4\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528print\u8bed\u53e5\u68c0\u67e5\u53d8\u91cf\u503c\u3001\u5229\u7528Python\u5185\u7f6e\u7684pdb\u6a21\u5757\u8fdb\u884c\u9010\u884c\u8c03\u8bd5\uff0c\u4ee5\u53ca\u4f7f\u7528IDE\uff08\u5982PyCharm\u6216VSCode\uff09\u63d0\u4f9b\u7684\u8c03\u8bd5\u5de5\u5177\u6765\u8bbe\u7f6e\u65ad\u70b9\u548c\u89c2\u5bdf\u53d8\u91cf\u3002\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u591f\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u6e05\u695a\u5730\u4e86\u89e3\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u7684\u95ee\u9898\u6240\u5728\u3002<\/p>\n<p><strong>\u5e38\u89c1\u7684Python\u9519\u8bef\u7c7b\u578b\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5728Python\u7f16\u7a0b\u4e2d\uff0c\u5f00\u53d1\u8005\u53ef\u80fd\u4f1a\u9047\u5230\u591a\u79cd\u9519\u8bef\u7c7b\u578b\u3002\u6700\u5e38\u89c1\u7684\u5305\u62ecSyntaxError\uff08\u8bed\u6cd5\u9519\u8bef\uff09\u3001TypeError\uff08\u7c7b\u578b\u9519\u8bef\uff09\u3001IndexError\uff08\u7d22\u5f15\u9519\u8bef\uff09\u548cValueError\uff08\u503c\u9519\u8bef\uff09\u3002\u4e86\u89e3\u8fd9\u4e9b\u9519\u8bef\u7684\u5177\u4f53\u542b\u4e49\u548c\u4ea7\u751f\u539f\u56e0\uff0c\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u5feb\u901f\u5730\u5b9a\u4f4d\u548c\u4fee\u590d\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u4f55\u6709\u6548\u5730\u5904\u7406Python\u4e2d\u7684\u5f02\u5e38\uff1f<\/strong><br \/>\u4f7f\u7528try-except\u8bed\u53e5\u53ef\u4ee5\u6709\u6548\u5904\u7406Python\u4e2d\u7684\u5f02\u5e38\u3002\u901a\u8fc7\u5c06\u53ef\u80fd\u51fa\u9519\u7684\u4ee3\u7801\u653e\u5728try\u5757\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5728except\u5757\u4e2d\u6355\u83b7\u548c\u5904\u7406\u9519\u8bef\u3002\u6b64\u5916\uff0c\u4f7f\u7528finally\u5757\u53ef\u4ee5\u786e\u4fdd\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c\u67d0\u4e9b\u4ee3\u7801\u59cb\u7ec8\u4f1a\u6267\u884c\u3002\u8fd9\u79cd\u7ed3\u6784\u80fd\u591f\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u7f16\u7a0b\u4e2d\u66f4\u6539\u9519\u8bef\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u7406\u89e3\u9519\u8bef\u4fe1\u606f\u3001\u8c03\u8bd5\u4ee3\u7801\u3001\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u3001\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b\u3001\u5b66\u4e60\u5e38\u89c1\u9519\u8bef\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1094581,"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\/1094575"}],"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=1094575"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094575\/revisions"}],"predecessor-version":[{"id":1094582,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094575\/revisions\/1094582"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1094581"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1094575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1094575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1094575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}