{"id":1107231,"date":"2025-01-08T16:47:42","date_gmt":"2025-01-08T08:47:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1107231.html"},"modified":"2025-01-08T16:47:44","modified_gmt":"2025-01-08T08:47:44","slug":"python%e5%91%bd%e4%bb%a4%e8%a1%8cif%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1107231.html","title":{"rendered":"python\u547d\u4ee4\u884cif\u5982\u4f55\u4f7f\u7528\u65b9\u6cd5"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071343\/e30cf74c-49bd-47e2-90b8-74ba4076c488.webp\" alt=\"python\u547d\u4ee4\u884cif\u5982\u4f55\u4f7f\u7528\u65b9\u6cd5\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u547d\u4ee4\u884cif\u8bed\u53e5\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a\u4f7f\u7528\u6761\u4ef6\u8868\u8fbe\u5f0f\u3001\u4f7f\u7528sys\u6a21\u5757\u4f20\u9012\u547d\u4ee4\u884c\u53c2\u6570\u3001\u4f7f\u7528argparse\u6a21\u5757\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u3002<\/strong> \u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5176\u4e2d\u7684\u4e00\u4e2a\u5173\u952e\u70b9\uff0c\u5373\u4f7f\u7528\u6761\u4ef6\u8868\u8fbe\u5f0f\u4ee5\u53ca\u5982\u4f55\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u6761\u4ef6\u8868\u8fbe\u5f0f<\/p>\n<\/p>\n<p><p>\u6761\u4ef6\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u7b80\u5355\u7684if\u5224\u65ad\u3002\u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u57fa\u672c\u8bed\u6cd5\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">&lt;expression&gt; if &lt;condition&gt; else &lt;expression&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5728\u547d\u4ee4\u884c\u4e2d\u8fdb\u884c\u7b80\u5355\u7684if\u5224\u65ad\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Even&quot;) if int(input(&quot;Enter a number: &quot;)) % 2 == 0 else print(&quot;Odd&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528sys\u6a21\u5757\u4f20\u9012\u547d\u4ee4\u884c\u53c2\u6570<\/p>\n<\/p>\n<p><p>Python\u7684sys\u6a21\u5757\u5141\u8bb8\u6211\u4eec\u901a\u8fc7\u547d\u4ee4\u884c\u4f20\u9012\u53c2\u6570\uff0c\u5e76\u5728\u811a\u672c\u4e2d\u5bf9\u8fd9\u4e9b\u53c2\u6570\u8fdb\u884c\u5904\u7406\u3002sys.argv\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u547d\u4ee4\u884c\u53c2\u6570\u3002\u7b2c\u4e00\u4e2a\u5143\u7d20sys.argv[0]\u662f\u811a\u672c\u540d\u79f0\uff0c\u540e\u7eed\u5143\u7d20\u662f\u4f20\u9012\u7684\u53c2\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>if len(sys.argv) &gt; 1:<\/p>\n<p>    if sys.argv[1] == &#39;hello&#39;:<\/p>\n<p>        print(&quot;Hello World&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;Argument not recognized&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;No arguments provided&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528argparse\u6a21\u5757\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570<\/p>\n<\/p>\n<p><p>argparse\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7528\u4e8e\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u7684\u6a21\u5757\uff0c\u652f\u6301\u66f4\u591a\u7684\u53c2\u6570\u7c7b\u578b\u548c\u9009\u9879\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import argparse<\/p>\n<p>parser = argparse.ArgumentParser(description=&quot;Process some integers.&quot;)<\/p>\n<p>parser.add_argument(&#39;integers&#39;, metavar=&#39;N&#39;, type=int, nargs=&#39;+&#39;, help=&#39;an integer for the accumulator&#39;)<\/p>\n<p>parser.add_argument(&#39;--sum&#39;, dest=&#39;accumulate&#39;, action=&#39;store_const&#39;, const=sum, default=max, help=&#39;sum the integers (default: find the max)&#39;)<\/p>\n<p>args = parser.parse_args()<\/p>\n<p>print(args.accumulate(args.integers))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u6761\u4ef6\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u903b\u8f91\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728\u5355\u884c\u4ee3\u7801\u4e2d\u4f7f\u7528\u6761\u4ef6\u8868\u8fbe\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = &quot;Even&quot; if number % 2 == 0 else &quot;Odd&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u7528\u6cd5\u975e\u5e38\u7b80\u6d01\uff0c\u9002\u7528\u4e8e\u9700\u8981\u7b80\u77ed\u8868\u8fbe\u6761\u4ef6\u5224\u65ad\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528sys\u6a21\u5757\u4f20\u9012\u547d\u4ee4\u884c\u53c2\u6570\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7sys\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u4f20\u9012\u591a\u4e2a\u53c2\u6570\uff0c\u5e76\u5728\u811a\u672c\u4e2d\u8fdb\u884c\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>if len(sys.argv) != 3:<\/p>\n<p>    print(&quot;Usage: script.py &lt;number1&gt; &lt;number2&gt;&quot;)<\/p>\n<p>    sys.exit(1)<\/p>\n<p>number1 = int(sys.argv[1])<\/p>\n<p>number2 = int(sys.argv[2])<\/p>\n<p>if number1 &gt; number2:<\/p>\n<p>    print(f&quot;{number1} is greater than {number2}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{number1} is not greater than {number2}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f20\u9012\u4e24\u4e2a\u53c2\u6570\uff0c\u5e76\u4f7f\u7528if\u8bed\u53e5\u6bd4\u8f83\u5b83\u4eec\u7684\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528argparse\u6a21\u5757\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>argparse\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u5904\u7406\u590d\u6742\u7684\u547d\u4ee4\u884c\u53c2\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u9ad8\u7ea7\u4f8b\u5b50\uff1a<\/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>    parser = argparse.ArgumentParser(description=&quot;A simple example of argparse&quot;)<\/p>\n<p>    parser.add_argument(&#39;-n&#39;, &#39;--number&#39;, type=int, required=True, help=&quot;A required integer&quot;)<\/p>\n<p>    parser.add_argument(&#39;-s&#39;, &#39;--string&#39;, type=str, help=&quot;An optional string&quot;)<\/p>\n<p>    parser.add_argument(&#39;--flag&#39;, action=&#39;store_true&#39;, help=&quot;A boolean flag&quot;)<\/p>\n<p>    args = parser.parse_args()<\/p>\n<p>    if args.flag:<\/p>\n<p>        print(&quot;Flag is set&quot;)<\/p>\n<p>    if args.string:<\/p>\n<p>        print(f&quot;String provided: {args.string}&quot;)<\/p>\n<p>    print(f&quot;Number provided: {args.number}&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528argparse\u5904\u7406\u5fc5\u9700\u53c2\u6570\u3001\u53ef\u9009\u53c2\u6570\u548c\u5e03\u5c14\u6807\u5fd7\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5e38\u89c1\u9519\u8bef\u548c\u8c03\u8bd5\u6280\u5de7<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u53c2\u6570\u7c7b\u578b\u9519\u8bef<\/strong>\uff1a\u786e\u4fdd\u4f20\u9012\u7684\u53c2\u6570\u7c7b\u578b\u4e0e\u9884\u671f\u4e00\u81f4\uff0c\u4f8b\u5982\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/li>\n<li><strong>\u53c2\u6570\u7f3a\u5931<\/strong>\uff1a\u68c0\u67e5\u662f\u5426\u63d0\u4f9b\u4e86\u6240\u6709\u5fc5\u9700\u7684\u53c2\u6570\u3002<\/li>\n<li><strong>\u53c2\u6570\u51b2\u7a81<\/strong>\uff1a\u907f\u514d\u4f7f\u7528\u76f8\u540c\u7684\u53c2\u6570\u540d\u79f0\u6216\u7f29\u5199\u3002<\/li>\n<\/ol>\n<p><p>\u8c03\u8bd5\u6280\u5de7\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528print\u8bed\u53e5<\/strong>\uff1a\u5728\u5173\u952e\u4f4d\u7f6e\u6dfb\u52a0print\u8bed\u53e5\uff0c\u8f93\u51fa\u53c2\u6570\u503c\u548c\u72b6\u6001\u4fe1\u606f\u3002<\/li>\n<li><strong>\u68c0\u67e5\u53c2\u6570\u957f\u5ea6<\/strong>\uff1a\u4f7f\u7528len()\u51fd\u6570\u68c0\u67e5sys.argv\u7684\u957f\u5ea6\uff0c\u786e\u4fdd\u4f20\u9012\u4e86\u6b63\u786e\u6570\u91cf\u7684\u53c2\u6570\u3002<\/li>\n<li><strong>\u4f7f\u7528try-except\u5757<\/strong>\uff1a\u6355\u83b7\u53ef\u80fd\u7684\u5f02\u5e38\uff0c\u5e76\u8f93\u51fa\u6709\u7528\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/li>\n<\/ol>\n<p><h3>\u516b\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u975e\u5e38\u5e38\u89c1\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u811a\u672c\u81ea\u52a8\u5316<\/strong>\uff1a\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u4f20\u9012\u914d\u7f6e\u548c\u9009\u9879\uff0c\u5b9e\u73b0\u811a\u672c\u81ea\u52a8\u5316\u3002<\/li>\n<li><strong>\u6570\u636e\u5904\u7406<\/strong>\uff1a\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u4f20\u9012\u8f93\u5165\u6587\u4ef6\u548c\u8f93\u51fa\u6587\u4ef6\u8def\u5f84\uff0c\u5904\u7406\u6570\u636e\u6587\u4ef6\u3002<\/li>\n<li><strong>\u6d4b\u8bd5\u548c\u8c03\u8bd5<\/strong>\uff1a\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u4f20\u9012\u6d4b\u8bd5\u53c2\u6570\uff0c\u5feb\u901f\u6267\u884c\u4e0d\u540c\u7684\u6d4b\u8bd5\u7528\u4f8b\u3002<\/li>\n<\/ol>\n<p><p>\u603b\u7ed3\u6765\u8bf4\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u5305\u62ec\u6761\u4ef6\u8868\u8fbe\u5f0f\u3001sys\u6a21\u5757\u548cargparse\u6a21\u5757\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u662f\u975e\u5e38\u5e38\u89c1\u4e14\u91cd\u8981\u7684\u6280\u80fd\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u7f16\u5199\u66f4\u52a0\u7075\u6d3b\u548c\u5f3a\u5927\u7684\u811a\u672c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u547d\u4ee4\u884c\u4e2d\u7f16\u5199if\u8bed\u53e5\uff1f<\/strong><br \/>\u5728Python\u547d\u4ee4\u884c\u4e2d\uff0cif\u8bed\u53e5\u7528\u4e8e\u6839\u636e\u6761\u4ef6\u6267\u884c\u4e0d\u540c\u7684\u4ee3\u7801\u5757\u3002\u53ef\u4ee5\u76f4\u63a5\u8f93\u5165\u6761\u4ef6\u5224\u65ad\uff0c\u4f8b\u5982\uff1a<code>if 5 &gt; 3: print(&quot;5 is greater than 3&quot;)<\/code>\u3002\u786e\u4fdd\u5728if\u8bed\u53e5\u540e\u9762\u4f7f\u7528\u5192\u53f7\uff0c\u5e76\u4e14\u5728\u4e0b\u4e00\u884c\u7f29\u8fdb\u4ee3\u7801\u5757\u3002\u8fd9\u6837\uff0c\u5f53\u6761\u4ef6\u4e3a\u771f\u65f6\uff0c\u76f8\u5173\u4ee3\u7801\u5c06\u88ab\u6267\u884c\u3002<\/p>\n<p><strong>\u5728\u547d\u4ee4\u884c\u4e2d\u5982\u4f55\u4f7f\u7528\u591a\u91cdif\u8bed\u53e5\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528elif\u548celse\u6765\u5b9e\u73b0\u591a\u91cd\u6761\u4ef6\u5224\u65ad\u3002\u5728\u547d\u4ee4\u884c\u4e2d\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a  <\/p>\n<pre><code class=\"language-python\">if x &gt; 10: \n    print(&quot;x is greater than 10&quot;) \nelif x &lt; 10: \n    print(&quot;x is less than 10&quot;) \nelse: \n    print(&quot;x is equal to 10&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u7ed3\u6784\u5141\u8bb8\u4f60\u6839\u636e\u4e0d\u540c\u7684\u6761\u4ef6\u6267\u884c\u4e0d\u540c\u7684\u4ee3\u7801\u5757\uff0c\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u63a7\u5236\u6d41\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728if\u8bed\u53e5\u4e2d\u4f7f\u7528\u903b\u8f91\u8fd0\u7b97\u7b26\uff1f<\/strong><br \/>\u903b\u8f91\u8fd0\u7b97\u7b26\u5982and\u3001or\u548cnot\u53ef\u4ee5\u5e2e\u52a9\u4f60\u6784\u5efa\u66f4\u590d\u6742\u7684\u6761\u4ef6\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u540c\u65f6\u68c0\u67e5\u591a\u4e2a\u6761\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528and\uff1a  <\/p>\n<pre><code class=\"language-python\">if age &gt;= 18 and age &lt;= 65: \n    print(&quot;You are in the working age group.&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u53ea\u6709\u5f53age\u572818\u523065\u4e4b\u95f4\u65f6\uff0c\u76f8\u5173\u4ee3\u7801\u624d\u4f1a\u6267\u884c\u3002\u4f7f\u7528\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u53ef\u4ee5\u589e\u5f3a\u4f60\u7684\u6761\u4ef6\u5224\u65ad\u80fd\u529b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4f7f\u7528\u547d\u4ee4\u884cif\u8bed\u53e5\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a\u4f7f\u7528\u6761\u4ef6\u8868\u8fbe\u5f0f\u3001\u4f7f\u7528sys\u6a21\u5757\u4f20\u9012\u547d\u4ee4\u884c\u53c2\u6570\u3001\u4f7f\u7528argpar [&hellip;]","protected":false},"author":3,"featured_media":1107240,"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\/1107231"}],"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=1107231"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1107231\/revisions"}],"predecessor-version":[{"id":1107241,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1107231\/revisions\/1107241"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1107240"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1107231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1107231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1107231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}