{"id":296466,"date":"2024-05-20T16:02:34","date_gmt":"2024-05-20T08:02:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/296466.html"},"modified":"2024-05-20T16:03:01","modified_gmt":"2024-05-20T08:03:01","slug":"%e5%87%bd%e6%95%b0-if-%e5%a6%82%e4%bd%95%e5%b5%8c%e5%a5%97-or-%e5%92%8c-and","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/296466.html","title":{"rendered":"\u51fd\u6570 if \u5982\u4f55\u5d4c\u5957 or \u548c and"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27143857\/ebcc5ff7-2a51-4562-a4bc-5c65ecc79bc5.webp\" alt=\"\u51fd\u6570 if \u5982\u4f55\u5d4c\u5957 or \u548c and\" \/><\/p>\n<p><p>\u5728\u7f16\u7a0b\u4e2d\uff0c<code>if<\/code>\u8bed\u53e5\u662f\u57fa\u7840\u4e4b\u4e00\uff0c\u5b83\u7528\u4e8e\u57fa\u4e8e\u7279\u5b9a\u6761\u4ef6\u6267\u884c\u4ee3\u7801\u5757\u3002\u8981\u7075\u6d3b\u4f7f\u7528<code>if<\/code>\u8bed\u53e5\u5d4c\u5957<code>or<\/code>\u548c<code>and<\/code>\u903b\u8f91\u8fd0\u7b97\u7b26\uff0c\u6211\u4eec\u9700\u8981\u7406\u89e3\u8fd9\u4e9b\u64cd\u4f5c\u7b26\u5982\u4f55\u5e2e\u52a9\u6211\u4eec\u6784\u5efa\u590d\u6742\u7684\u6761\u4ef6\u8bed\u53e5\u3002<strong>\u5f53\u4f60\u4f7f\u7528<code>or<\/code>\u64cd\u4f5c\u7b26\u65f6\uff0c\u4f60\u7684\u6761\u4ef6\u4e4b\u95f4\u4e3a&quot;\u6216&quot;\u5173\u7cfb\uff0c\u53ea\u8981\u6709\u4e00\u4e2a\u6761\u4ef6\u4e3a\u771f\uff0c\u6574\u4e2a\u8868\u8fbe\u5f0f\u5c31\u4e3a\u771f\u3002\u4f7f\u7528<code>and<\/code>\u65f6\uff0c\u6761\u4ef6\u4e4b\u95f4\u4e3a&quot;\u4e0e&quot;\u5173\u7cfb\uff0c\u6240\u6709\u6761\u4ef6\u5fc5\u987b\u540c\u65f6\u4e3a\u771f\uff0c\u6574\u4e2a\u8868\u8fbe\u5f0f\u624d\u4e3a\u771f<\/strong>\u3002\u8fd9\u4e24\u79cd\u8fd0\u7b97\u7b26\u53ef\u4ee5\u5d4c\u5957\u5728<code>if<\/code>\u8bed\u53e5\u4e2d\uff0c\u7528\u4e8e\u6784\u5efa\u9700\u8981\u591a\u6761\u4ef6\u5224\u65ad\u7684\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><p>\u5728\u8be6\u7ec6\u63cf\u8ff0\u4e4b\u524d\uff0c\u5148\u6765\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a\u5047\u8bbe\u4f60\u6b63\u5728\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u8981\u68c0\u67e5\u67d0\u4e2a\u7528\u6237\u662f\u5426\u53ef\u4ee5\u8bbf\u95ee\u9ad8\u7ea7\u529f\u80fd\uff0c\u8fd9\u4e2a\u68c0\u67e5\u53ef\u80fd\u57fa\u4e8e\u7528\u6237\u7684\u5e74\u9f84<strong>\u548c<\/strong>\u8ba2\u9605\u7ea7\u522b\u3002\u8fd9\u91cc\uff0c\u5e74\u9f84\u53ef\u80fd\u9700\u8981\u5927\u4e8e\u7b49\u4e8e18\u5c81<strong>\u5e76\u4e14<\/strong>\u8ba2\u9605\u7ea7\u522b\u9700\u8981\u662f&quot;\u9ad8\u7ea7&quot;\uff0c\u6b64\u65f6\u5c31\u9700\u8981\u7528\u5230<code>and<\/code>\u64cd\u4f5c\u7b26\u3002\u4f46\u5982\u679c\u89c4\u5b9a\u53ea\u8981\u5e74\u9f84\u5927\u4e8e18\u5c81<strong>\u6216\u8005<\/strong>\u662fVIP\u7528\u6237\u5c31\u53ef\u4ee5\u8bbf\u95ee\u67d0\u4e9b\u7279\u5b9a\u529f\u80fd\uff0c\u8fd9\u5c31\u9700\u8981\u7528\u5230<code>or<\/code>\u64cd\u4f5c\u7b26\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u7406\u89e3 AND \u548c OR<\/h2>\n<\/p>\n<p><h3>AND \u64cd\u4f5c\u7b26<\/h3>\n<\/p>\n<p><p><code>AND<\/code>\u8fd0\u7b97\u7b26\u7528\u4e8e\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u6761\u4ef6\u8fde\u63a5\u8d77\u6765\uff0c\u53ea\u6709\u5f53\u6240\u6709\u6761\u4ef6\u90fd\u4e3atrue\u65f6\uff0c\u6574\u4e2a\u8868\u8fbe\u5f0f\u624d\u8ba1\u7b97\u4e3atrue\u3002\u8fd9\u5728\u9700\u8981\u540c\u65f6\u6ee1\u8db3\u591a\u4e2a\u6761\u4ef6\u7684\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u786e\u4fdd\u4e00\u4e2a\u6570\u5b57\u65e2\u662f\u5076\u6570\u53c8\u5728\u67d0\u4e2a\u8303\u56f4\u5185\u3002<\/p>\n<\/p>\n<p><h3>OR\u64cd\u4f5c\u7b26<\/h3>\n<\/p>\n<p><p>\u76f8\u53cd\u5730\uff0c<code>OR<\/code>\u8fd0\u7b97\u7b26\u7528\u4e8e\u5904\u7406\u81f3\u5c11\u6709\u4e00\u4e2a\u6761\u4ef6\u4e3a\u771f\u65f6\u6574\u4e2a\u8868\u8fbe\u5f0f\u4e3a\u771f\u7684\u60c5\u51b5\u3002\u8fd9\u5bf9\u4e8e\u63d0\u4f9b\u66ff\u4ee3\u6761\u4ef6\u975e\u5e38\u6709\u7528\uff0c\u6bd4\u5982\u5141\u8bb8\u7528\u6237\u57fa\u4e8e\u7528\u6237\u540d\u6216\u7535\u5b50\u90ae\u4ef6\u767b\u5f55\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u5d4c\u5957 IF \u8bed\u53e5\u4e2d\u7684 AND \u548c OR<\/h2>\n<\/p>\n<p><p>\u5d4c\u5957<code>if<\/code>\u8bed\u53e5\u65f6\uff0c<code>and<\/code>\u548c<code>or<\/code>\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u51cf\u5c11\u4ee3\u7801\u7684\u590d\u6742\u6027\uff0c\u901a\u8fc7\u4e00\u6761\u8bed\u53e5\u9a8c\u8bc1\u591a\u4e2a\u6761\u4ef6\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u80fd\u8981\u68c0\u67e5\u4e00\u4e2a\u8ba2\u5355\u662f\u5426\u6ee1\u8db3\u514d\u8d39\u9001\u8d27\u7684\u6761\u4ef6\uff1a\u8ba2\u5355\u603b\u989d\u8d85\u8fc7\u4e00\u5b9a\u91d1\u989d<strong>\u5e76\u4e14<\/strong>\u76ee\u7684\u5730\u5728\u7279\u5b9a\u5730\u57df\u5185\u3002\u8fd9\u91cc\u5c31\u53ef\u4ee5\u4f7f\u7528<code>and<\/code>\u64cd\u4f5c\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u590d\u5408\u6761\u4ef6\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u5728\u63d0\u9ad8\u590d\u6742\u6027\u7684\u6761\u4ef6\u5224\u65ad\u65f6\uff0c<code>and<\/code>\u548c<code>or<\/code>\u7684\u7ec4\u5408\u4f7f\u7528\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002\u8003\u8651\u5230\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\uff0c\u6b63\u786e\u7684\u5c06\u8fd9\u4e9b\u903b\u8f91\u8fd0\u7b97\u7b26\u5d4c\u5957\u5728<code>if<\/code>\u8bed\u53e5\u4e2d\u662f\u4e00\u4e2a\u6280\u5de7\u6d3b\u3002<\/p>\n<\/p>\n<p><h3>\u907f\u514d\u903b\u8f91\u9677\u9631<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>and<\/code>\u548c<code>or<\/code>\u65f6\uff0c\u5c24\u5176\u662f\u5728\u76f8\u4e92\u6df7\u7528\u65f6\uff0c\u8981\u6ce8\u610f\u903b\u8f91\u7684\u6b63\u786e\u6027\u3002\u9519\u8bef\u5730\u5b89\u6392\u8fd9\u4e9b\u64cd\u4f5c\u7b26\u53ef\u80fd\u4f1a\u5bfc\u81f4\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u5b9e\u8df5\u6848\u4f8b<\/h2>\n<\/p>\n<p><p>\u4e3a\u4e86\u6df1\u5316\u7406\u89e3\uff0c\u6211\u4eec\u6765\u770b\u51e0\u4e2a\u5b9e\u8df5\u4e2d\u7684\u6848\u4f8b\u3002\u901a\u8fc7\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u6211\u4eec\u80fd\u66f4\u597d\u5730\u7406\u89e3<code>and<\/code>\u548c<code>or<\/code>\u5728<code>if<\/code>\u8bed\u53e5\u4e2d\u7684\u5d4c\u5957\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u7528\u6237\u6743\u9650\u9a8c\u8bc1<\/h3>\n<\/p>\n<p><p>\u9a8c\u8bc1\u7528\u6237\u662f\u5426\u6709\u6743\u9650\u8bbf\u95ee\u7279\u5b9a\u8d44\u6e90\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u78b0\u5230\u9700\u8981\u6839\u636e\u7528\u6237\u7684\u591a\u9879\u5c5e\u6027\u505a\u51b3\u5b9a\u7684\u60c5\u51b5\u3002\u8fd9\u65f6\uff0c<code>and<\/code>\u548c<code>or<\/code>\u7684\u4f7f\u7528\u5c31\u53d8\u5f97\u975e\u5e38\u5173\u952e\u3002<\/p>\n<\/p>\n<p><h3>\u6570\u636e\u9a8c\u8bc1<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u8f93\u5165\u9a8c\u8bc1\u7684\u573a\u666f\u4e2d\uff0c\u6211\u4eec\u4e5f\u7ecf\u5e38\u9700\u8981\u6839\u636e\u591a\u4e2a\u6761\u4ef6\u6765\u51b3\u5b9a\u6570\u636e\u662f\u5426\u6709\u6548\u3002\u901a\u8fc7\u667a\u80fd\u5730\u4f7f\u7528<code>and<\/code>\u548c<code>or<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u66f4\u52a0\u6e05\u6670\u548c\u6709\u6548\u7684\u9a8c\u8bc1\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u6700\u4f73\u5b9e\u8df5<\/h2>\n<\/p>\n<p><h3>\u4fdd\u6301\u6e05\u6670\u7b80\u6d01<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>and<\/code>\u548c<code>or<\/code>\u65f6\uff0c\u5c3d\u91cf\u4fdd\u6301\u6761\u4ef6\u5224\u65ad\u8bed\u53e5\u7684\u7b80\u6d01\u660e\u4e86\u3002\u8fc7\u4e8e\u590d\u6742\u7684\u903b\u8f91\u4e0d\u4ec5\u9605\u8bfb\u56f0\u96be\uff0c\u800c\u4e14\u6613\u4e8e\u51fa\u9519\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u62ec\u53f7<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u907f\u514d\u903b\u8f91\u6df7\u6dc6\uff0c\u660e\u667a\u5730\u4f7f\u7528\u62ec\u53f7\u6765\u660e\u786e\u64cd\u4f5c\u7b26\u7684\u4f18\u5148\u7ea7\u662f\u975e\u5e38\u6709\u5e2e\u52a9\u7684\u3002\u8fd9\u53ef\u4ee5\u786e\u4fdd\u6211\u4eec\u7684\u6761\u4ef6\u6309\u7167\u671f\u671b\u7684\u65b9\u5f0f\u88ab\u8bc4\u4f30\u3002<\/p>\n<\/p>\n<p><p> \u901a\u8fc7\u4ee5\u4e0a\u7684\u4ecb\u7ecd\u548c\u6848\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u5d4c\u5957<code>if<\/code>\u8bed\u53e5\u4e2d\u6b63\u786e\u5730\u4f7f\u7528<code>and<\/code>\u548c<code>or<\/code>\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u6784\u5efa\u51fa\u65e2\u5f3a\u5927\u53c8\u7075\u6d3b\u7684\u6761\u4ef6\u5224\u65ad\u903b\u8f91\u3002\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\uff0c\u5bf9\u4e8e\u7f16\u5199\u9ad8\u8d28\u91cf\u3001\u6613\u4e8e\u7ef4\u62a4\u7684\u4ee3\u7801\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u5728\u5d4c\u5957\u7684 if \u8bed\u53e5\u4e2d\u4f7f\u7528 or \u64cd\u4f5c\u7b26\uff1f<\/strong><\/p>\n<p>\u5728\u5d4c\u5957\u7684 if \u8bed\u53e5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 or \u64cd\u4f5c\u7b26\u6765\u7ec4\u5408\u591a\u4e2a\u6761\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">x = 5\n\nif x &gt; 10 or x &lt; 3:\n    print(&quot;x \u5927\u4e8e 10 \u6216\u5c0f\u4e8e 3&quot;)\nelse:\n    print(&quot;x \u5728 3 \u548c 10 \u4e4b\u95f4&quot;)\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5982\u679c x \u7684\u503c\u5927\u4e8e 10 \u6216\u5c0f\u4e8e 3\uff0c\u5c31\u4f1a\u8fdb\u5165\u7b2c\u4e00\u4e2a if \u8bed\u53e5\u5757\uff0c\u5426\u5219\u4f1a\u6267\u884c else \u5757\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u5728\u5d4c\u5957\u7684 if \u8bed\u53e5\u4e2d\u4f7f\u7528 and \u64cd\u4f5c\u7b26\uff1f<\/strong><\/p>\n<p>\u5728\u5d4c\u5957\u7684 if \u8bed\u53e5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 and \u64cd\u4f5c\u7b26\u6765\u540c\u65f6\u6ee1\u8db3\u591a\u4e2a\u6761\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">x = 5\ny = 8\n\nif x &gt; 3 and y &lt; 10:\n    print(&quot;x \u5927\u4e8e 3 \u5e76\u4e14 y \u5c0f\u4e8e 10&quot;)\nelse:\n    print(&quot;x \u5c0f\u4e8e\u7b49\u4e8e 3 \u6216 y \u5927\u4e8e\u7b49\u4e8e 10&quot;)\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u53ea\u6709\u5f53 x \u7684\u503c\u5927\u4e8e 3 \u5e76\u4e14 y \u7684\u503c\u5c0f\u4e8e 10 \u65f6\uff0c\u624d\u4f1a\u8fdb\u5165\u7b2c\u4e00\u4e2a if \u8bed\u53e5\u5757\uff0c\u5426\u5219\u4f1a\u6267\u884c else \u5757\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u5728\u5d4c\u5957\u7684 if \u8bed\u53e5\u4e2d\u540c\u65f6\u4f7f\u7528 or \u548c and \u64cd\u4f5c\u7b26\uff1f<\/strong><\/p>\n<p>\u5728\u5d4c\u5957\u7684 if \u8bed\u53e5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u540c\u65f6\u4f7f\u7528 or \u548c and \u64cd\u4f5c\u7b26\u6765\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6761\u4ef6\u5224\u65ad\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">x = 5\ny = 8\nz = 12\n\nif (x &gt; 3 and y &lt; 10) or z == 12:\n    print(&quot;x \u5927\u4e8e 3 \u5e76\u4e14 y \u5c0f\u4e8e 10\uff0c\u6216\u8005 z \u7b49\u4e8e 12&quot;)\nelse:\n    print(&quot;\u6761\u4ef6\u4e0d\u6ee1\u8db3&quot;)\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5982\u679c x \u7684\u503c\u5927\u4e8e 3 \u5e76\u4e14 y \u7684\u503c\u5c0f\u4e8e 10\uff0c\u6216\u8005 z \u7684\u503c\u7b49\u4e8e 12\uff0c\u5c31\u4f1a\u8fdb\u5165\u7b2c\u4e00\u4e2a if \u8bed\u53e5\u5757\uff0c\u5426\u5219\u4f1a\u6267\u884c else \u5757\u3002\u5728\u5d4c\u5957\u7684 if \u8bed\u53e5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u7ec4\u5408\u4e0d\u540c\u7684\u64cd\u4f5c\u7b26\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6761\u4ef6\u5224\u65ad\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728\u7f16\u7a0b\u4e2d\uff0cif\u8bed\u53e5\u662f\u57fa\u7840\u4e4b\u4e00\uff0c\u5b83\u7528\u4e8e\u57fa\u4e8e\u7279\u5b9a\u6761\u4ef6\u6267\u884c\u4ee3\u7801\u5757\u3002\u8981\u7075\u6d3b\u4f7f\u7528if\u8bed\u53e5\u5d4c\u5957or\u548cand\u903b\u8f91\u8fd0\u7b97\u7b26\uff0c\u6211\u4eec [&hellip;]","protected":false},"author":3,"featured_media":296490,"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\/296466"}],"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=296466"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/296466\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/296490"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=296466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=296466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=296466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}