{"id":1159924,"date":"2025-01-13T18:58:45","date_gmt":"2025-01-13T10:58:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1159924.html"},"modified":"2025-01-13T18:58:47","modified_gmt":"2025-01-13T10:58:47","slug":"elif%e5%9c%a8python%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1159924.html","title":{"rendered":"elif\u5728python\u4e2d\u5982\u4f55\u4f7f\u7528"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25201532\/69ea3476-2705-48eb-9ba0-d04988129d64.webp\" alt=\"elif\u5728python\u4e2d\u5982\u4f55\u4f7f\u7528\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c<code>elif<\/code> \u662f <code>if<\/code> \u8bed\u53e5\u7684\u4e00\u4e2a\u5ef6\u7eed\uff0c\u7528\u4e8e\u68c0\u67e5\u591a\u4e2a\u6761\u4ef6\u3002<strong>elif\u7528\u4e8e\u5728\u7b2c\u4e00\u4e2a\u6761\u4ef6\u4e0d\u6ee1\u8db3\u7684\u60c5\u51b5\u4e0b\uff0c\u68c0\u67e5\u5176\u4ed6\u591a\u4e2a\u6761\u4ef6<\/strong>\uff0c\u4ece\u800c\u5b9e\u73b0\u591a\u91cd\u5206\u652f\u7684\u903b\u8f91\u5224\u65ad\u3002<strong>elif\u5173\u952e\u8bcd\u53ef\u4ee5\u907f\u514d\u591a\u4e2a\u5d4c\u5957\u7684if-else\u8bed\u53e5\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3001\u6613\u8bfb<\/strong>\u3002\u6211\u4eec\u5c06\u8be6\u7ec6\u8bf4\u660e\u5982\u4f55\u4f7f\u7528elif\uff0c\u901a\u8fc7\u5177\u4f53\u793a\u4f8b\u6765\u89e3\u91ca\u5176\u5de5\u4f5c\u673a\u5236\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>elif<\/code> \u7684\u57fa\u672c\u8bed\u6cd5\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if \u6761\u4ef61:<\/p>\n<p>    \u8bed\u53e5\u57571<\/p>\n<p>elif \u6761\u4ef62:<\/p>\n<p>    \u8bed\u53e5\u57572<\/p>\n<p>elif \u6761\u4ef63:<\/p>\n<p>    \u8bed\u53e5\u57573<\/p>\n<p>else:<\/p>\n<p>    \u8bed\u53e5\u57574<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5f53<code>\u6761\u4ef61<\/code>\u6210\u7acb\u65f6\uff0c\u6267\u884c<code>\u8bed\u53e5\u57571<\/code>\uff1b\u5982\u679c\u4e0d\u6210\u7acb\uff0c\u5219\u68c0\u67e5<code>\u6761\u4ef62<\/code>\uff0c\u5982\u679c<code>\u6761\u4ef62<\/code>\u6210\u7acb\uff0c\u5219\u6267\u884c<code>\u8bed\u53e5\u57572<\/code>\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002\u5982\u679c\u6240\u6709\u7684\u6761\u4ef6\u90fd\u4e0d\u6210\u7acb\uff0c\u5219\u6267\u884c <code>else<\/code> \u5757\u4e2d\u7684\u8bed\u53e5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u793a\u4f8b\u89e3\u91ca<\/h3>\n<\/p>\n<p><h4>\u793a\u4f8b\u4e00\uff1a\u7b80\u5355\u7684\u5206\u6570\u7b49\u7ea7\u5224\u65ad<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">score = 85<\/p>\n<p>if score &gt;= 90:<\/p>\n<p>    print(&quot;A&quot;)<\/p>\n<p>elif score &gt;= 80:<\/p>\n<p>    print(&quot;B&quot;)<\/p>\n<p>elif score &gt;= 70:<\/p>\n<p>    print(&quot;C&quot;)<\/p>\n<p>elif score &gt;= 60:<\/p>\n<p>    print(&quot;D&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;F&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u6839\u636e <code>score<\/code> \u7684\u503c\u6765\u5224\u65ad\u5b66\u751f\u7684\u6210\u7ee9\u7b49\u7ea7\u3002\u5982\u679c <code>score<\/code> \u5927\u4e8e\u7b49\u4e8e90\uff0c\u7a0b\u5e8f\u4f1a\u8f93\u51fa &quot;A&quot;\uff1b\u5982\u679c\u5927\u4e8e\u7b49\u4e8e80\u4f46\u5c0f\u4e8e90\uff0c\u8f93\u51fa &quot;B&quot;\uff0c\u4f9d\u6b64\u7c7b\u63a8\u3002<strong>\u901a\u8fc7\u4f7f\u7528elif\uff0c\u907f\u514d\u4e86\u591a\u4e2a\u5d4c\u5957if-else\u8bed\u53e5\uff0c\u4f7f\u4ee3\u7801\u6e05\u6670\u660e\u4e86<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u591a\u4e2a\u6761\u4ef6\u7684\u5224\u65ad<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u591a\u4e2a\u6761\u4ef6\u8fdb\u884c\u5224\u65ad\uff0c\u8fd9\u65f6\u53ef\u4ee5\u7ed3\u5408 <code>elif<\/code> \u548c\u903b\u8f91\u8fd0\u7b97\u7b26\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4e8c\uff1a\u590d\u6742\u6761\u4ef6\u7684\u5224\u65ad<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">age = 25<\/p>\n<p>income = 50000<\/p>\n<p>if age &lt; 18:<\/p>\n<p>    print(&quot;\u672a\u6210\u5e74\u4eba&quot;)<\/p>\n<p>elif age &gt;= 18 and age &lt; 60 and income &lt; 20000:<\/p>\n<p>    print(&quot;\u6210\u5e74\u4f4e\u6536\u5165&quot;)<\/p>\n<p>elif age &gt;= 18 and age &lt; 60 and income &gt;= 20000:<\/p>\n<p>    print(&quot;\u6210\u5e74\u4e2d\u9ad8\u6536\u5165&quot;)<\/p>\n<p>elif age &gt;= 60 and income &lt; 20000:<\/p>\n<p>    print(&quot;\u8001\u5e74\u4f4e\u6536\u5165&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u8001\u5e74\u4e2d\u9ad8\u6536\u5165&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u6839\u636e <code>age<\/code> \u548c <code>income<\/code> \u7684\u503c\u6765\u5224\u65ad\u4e00\u4e2a\u4eba\u7684\u6536\u5165\u6c34\u5e73\u548c\u5e74\u9f84\u9636\u6bb5\u3002<strong>\u901a\u8fc7\u4f7f\u7528\u903b\u8f91\u8fd0\u7b97\u7b26\u7ed3\u5408elif\uff0c\u53ef\u4ee5\u5bf9\u591a\u91cd\u6761\u4ef6\u8fdb\u884c\u51c6\u786e\u5224\u65ad<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u907f\u514d\u5e38\u89c1\u9519\u8bef<\/h3>\n<\/p>\n<p><h4>1\u3001\u7f29\u8fdb\u9519\u8bef<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u7f29\u8fdb\u975e\u5e38\u91cd\u8981\u3002\u6bcf\u4e2a<code>if<\/code>\u3001<code>elif<\/code>\u548c<code>else<\/code>\u5757\u4e2d\u7684\u8bed\u53e5\u5fc5\u987b\u6b63\u786e\u7f29\u8fdb\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4\u8bed\u6cd5\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9519\u8bef\u793a\u4f8b<\/p>\n<p>if condition:<\/p>\n<p>print(&quot;This will cause an IndentationError&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u9057\u6f0f<code>else<\/code>\u5757<\/h4>\n<\/p>\n<p><p>\u867d\u7136<code>else<\/code>\u5757\u4e0d\u662f\u5fc5\u987b\u7684\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9057\u6f0f<code>else<\/code>\u5757\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u65e0\u6cd5\u5904\u7406\u6240\u6709\u60c5\u51b5\uff0c\u4ece\u800c\u4ea7\u751f\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9519\u8bef\u793a\u4f8b<\/p>\n<p>if condition1:<\/p>\n<p>    print(&quot;Condition 1 is True&quot;)<\/p>\n<p>elif condition2:<\/p>\n<p>    print(&quot;Condition 2 is True&quot;)<\/p>\n<h2><strong>\u5982\u679ccondition1\u548ccondition2\u90fd\u4e0d\u6210\u7acb\uff0c\u7a0b\u5e8f\u5c06\u4e0d\u4f1a\u6709\u4efb\u4f55\u8f93\u51fa<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>\u793a\u4f8b\u4e09\uff1a\u57fa\u4e8e\u7528\u6237\u8f93\u5165\u7684\u591a\u91cd\u6761\u4ef6\u5224\u65ad<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">user_role = input(&quot;\u8bf7\u8f93\u5165\u4f60\u7684\u89d2\u8272 (admin\/user\/guest): &quot;)<\/p>\n<p>if user_role == &quot;admin&quot;:<\/p>\n<p>    print(&quot;\u4f60\u6709\u6700\u9ad8\u6743\u9650&quot;)<\/p>\n<p>elif user_role == &quot;user&quot;:<\/p>\n<p>    print(&quot;\u4f60\u6709\u666e\u901a\u6743\u9650&quot;)<\/p>\n<p>elif user_role == &quot;guest&quot;:<\/p>\n<p>    print(&quot;\u4f60\u6709\u8bbf\u5ba2\u6743\u9650&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u89d2\u8272\u672a\u77e5\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u6839\u636e\u7528\u6237\u8f93\u5165\u7684\u89d2\u8272\u6765\u5224\u65ad\u5e76\u8f93\u51fa\u76f8\u5e94\u7684\u6743\u9650\u7b49\u7ea7\u3002<strong>\u4f7f\u7528elif\u5b9e\u73b0\u4e86\u5bf9\u591a\u79cd\u7528\u6237\u89d2\u8272\u7684\u5224\u65ad\uff0c\u4f7f\u5f97\u4ee3\u7801\u903b\u8f91\u975e\u5e38\u6e05\u6670<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\u548c\u8bb2\u89e3\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230<code>elif<\/code>\u5728Python\u7f16\u7a0b\u4e2d\u7684\u91cd\u8981\u6027\u548c\u5e94\u7528\u573a\u666f\u3002\u5b83\u4e0d\u4ec5\u7b80\u5316\u4e86\u4ee3\u7801\u7ed3\u6784\uff0c\u8fd8\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002<strong>\u5408\u7406\u4f7f\u7528elif\uff0c\u53ef\u4ee5\u4f7f\u7a0b\u5e8f\u903b\u8f91\u66f4\u52a0\u6e05\u6670\u3001\u7b80\u6d01<\/strong>\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u8d28\u91cf\u548c\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0c\u638c\u63e1\u5e76\u7075\u6d3b\u8fd0\u7528<code>elif<\/code>\u8bed\u53e5\uff0c\u662f\u6bcf\u4e00\u4e2aPython\u7a0b\u5e8f\u5458\u90fd\u9700\u8981\u5177\u5907\u7684\u57fa\u672c\u6280\u80fd\u3002\u901a\u8fc7\u4e0d\u65ad\u7684\u5b9e\u8df5\u548c\u4f18\u5316\uff0c\u4f60\u5c06\u80fd\u591f\u7f16\u5199\u51fa\u66f4\u52a0\u9ad8\u6548\u3001\u4f18\u96c5\u7684Python\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>1. \u5728Python\u4e2d\uff0c<code>elif<\/code>\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f<\/strong><br \/><code>elif<\/code>\u662fPython\u4e2d\u7684\u4e00\u4e2a\u6761\u4ef6\u8bed\u53e5\uff0c\u7528\u4e8e\u5728\u591a\u4e2a\u6761\u4ef6\u4e2d\u8fdb\u884c\u5224\u65ad\u3002\u5b83\u7684\u5168\u79f0\u662f\u201celse if\u201d\uff0c\u53ef\u4ee5\u5728<code>if<\/code>\u8bed\u53e5\u540e\u9762\u6dfb\u52a0\u4e00\u4e2a\u6216\u591a\u4e2a<code>elif<\/code>\u6765\u5904\u7406\u4e0d\u540c\u7684\u6761\u4ef6\u3002\u5f53\u7b2c\u4e00\u4e2a<code>if<\/code>\u6761\u4ef6\u4e3aFalse\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u4f9d\u6b21\u68c0\u67e5\u6bcf\u4e2a<code>elif<\/code>\u6761\u4ef6\uff0c\u76f4\u5230\u627e\u5230\u4e00\u4e2a\u4e3aTrue\u7684\u6761\u4ef6\uff0c\u6216\u8005\u6240\u6709\u6761\u4ef6\u90fd\u88ab\u68c0\u67e5\u5b8c\u3002\u5982\u679c\u6ca1\u6709\u4efb\u4f55\u6761\u4ef6\u4e3aTrue\uff0c\u7a0b\u5e8f\u4f1a\u6267\u884c<code>else<\/code>\u90e8\u5206\uff08\u5982\u679c\u6709\u7684\u8bdd\uff09\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u5728<code>elif<\/code>\u8bed\u53e5\u4e2d\u4f7f\u7528\u591a\u4e2a\u6761\u4ef6\uff1f<\/strong><br \/>\u5728<code>elif<\/code>\u8bed\u53e5\u4e2d\u53ef\u4ee5\u4f7f\u7528\u903b\u8f91\u8fd0\u7b97\u7b26\uff08\u5982<code>and<\/code>\u3001<code>or<\/code>\uff09\u6765\u7ec4\u5408\u591a\u4e2a\u6761\u4ef6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5199\u6210<code>elif condition1 and condition2:<\/code>\uff0c\u8fd9\u6837\u53ea\u6709\u5728\u4e24\u4e2a\u6761\u4ef6\u90fd\u4e3aTrue\u65f6\uff0c\u624d\u4f1a\u6267\u884c\u76f8\u5e94\u7684\u4ee3\u7801\u5757\u3002\u4f7f\u7528\u903b\u8f91\u8fd0\u7b97\u7b26\u53ef\u4ee5\u4f7f\u6761\u4ef6\u5224\u65ad\u66f4\u52a0\u7075\u6d3b\uff0c\u9002\u5e94\u590d\u6742\u7684\u903b\u8f91\u9700\u6c42\u3002<\/p>\n<p><strong>3. \u4f7f\u7528<code>elif<\/code>\u65f6\uff0c\u662f\u5426\u53ef\u4ee5\u7701\u7565<code>else<\/code>\u90e8\u5206\uff1f<\/strong><br \/>\u662f\u7684\uff0c<code>elif<\/code>\u8bed\u53e5\u53ef\u4ee5\u5728\u6ca1\u6709<code>else<\/code>\u90e8\u5206\u7684\u60c5\u51b5\u4e0b\u6b63\u5e38\u5de5\u4f5c\u3002\u5982\u679c\u6240\u6709\u7684<code>if<\/code>\u548c<code>elif<\/code>\u6761\u4ef6\u90fd\u4e0d\u6ee1\u8db3\uff0c\u7a0b\u5e8f\u5c06\u7b80\u5355\u5730\u8df3\u8fc7\u8fd9\u4e9b\u6761\u4ef6\u8bed\u53e5\uff0c\u800c\u4e0d\u4f1a\u62a5\u9519\u3002\u56e0\u6b64\uff0c<code>else<\/code>\u90e8\u5206\u5e76\u4e0d\u662f\u5fc5\u9700\u7684\uff0c\u4f46\u5b83\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u6240\u6709\u6761\u4ef6\u90fd\u4e0d\u6ee1\u8db3\u65f6\u7684\u9ed8\u8ba4\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0celif \u662f if \u8bed\u53e5\u7684\u4e00\u4e2a\u5ef6\u7eed\uff0c\u7528\u4e8e\u68c0\u67e5\u591a\u4e2a\u6761\u4ef6\u3002elif\u7528\u4e8e\u5728\u7b2c\u4e00\u4e2a\u6761\u4ef6\u4e0d\u6ee1\u8db3\u7684\u60c5\u51b5\u4e0b [&hellip;]","protected":false},"author":3,"featured_media":1159927,"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\/1159924"}],"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=1159924"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159924\/revisions"}],"predecessor-version":[{"id":1159930,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159924\/revisions\/1159930"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1159927"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1159924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1159924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1159924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}