{"id":1168627,"date":"2025-01-15T16:00:26","date_gmt":"2025-01-15T08:00:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1168627.html"},"modified":"2025-01-15T16:00:29","modified_gmt":"2025-01-15T08:00:29","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a9%e8%af%ad%e5%8f%a5%e9%87%8d%e6%96%b0%e5%88%a4%e6%96%ad","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1168627.html","title":{"rendered":"python\u5982\u4f55\u8ba9\u8bed\u53e5\u91cd\u65b0\u5224\u65ad"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26065822\/b57b393a-1382-47ce-96af-65731ecf36d0.webp\" alt=\"python\u5982\u4f55\u8ba9\u8bed\u53e5\u91cd\u65b0\u5224\u65ad\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8ba9\u8bed\u53e5\u91cd\u65b0\u5224\u65ad\uff0c\u5982\u4f7f\u7528\u5faa\u73af\u3001\u9012\u5f52\u8c03\u7528\u3001\u5f02\u5e38\u5904\u7406\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528\u5faa\u73af\u662f\u6700\u5e38\u89c1\u548c\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5faa\u73af\u7ed3\u6784\uff0c\u53ef\u4ee5\u53cd\u590d\u6267\u884c\u67d0\u4e00\u4ee3\u7801\u5757\uff0c\u76f4\u5230\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u4e3a\u6b62\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u5faa\u73af\u91cd\u65b0\u5224\u65ad\u8bed\u53e5\u3002<\/p>\n<\/p>\n<p><p><strong>\u5faa\u73af\u7ed3\u6784\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5faa\u73af\u662f\u63a7\u5236\u7a0b\u5e8f\u6d41\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u91cd\u590d\u6267\u884c\u67d0\u4e9b\u4ee3\u7801\u5757\u3002Python\u4e2d\u4e3b\u8981\u6709\u4e24\u79cd\u5faa\u73af\u7ed3\u6784\uff1a<code>for<\/code>\u5faa\u73af\u548c<code>while<\/code>\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u4f7f\u7528while\u5faa\u73af<\/strong><\/p>\n<\/p>\n<p><p><code>while<\/code>\u5faa\u73af\u662f\u4e00\u79cd\u6761\u4ef6\u5faa\u73af\uff0c\u53ea\u8981\u6761\u4ef6\u4e3a\u771f\uff0c\u5b83\u5c31\u4f1a\u91cd\u590d\u6267\u884c\u4ee3\u7801\u5757\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u6765\u8ba9\u8bed\u53e5\u91cd\u65b0\u5224\u65ad\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u8ba9\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570\uff0c\u5982\u679c\u8f93\u5165\u65e0\u6548\uff0c\u5219\u91cd\u65b0\u8981\u6c42\u8f93\u5165<\/p>\n<p>while True:<\/p>\n<p>    try:<\/p>\n<p>        num = int(input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570: &quot;))<\/p>\n<p>        if num &gt; 0:<\/p>\n<p>            print(f&quot;\u60a8\u8f93\u5165\u7684\u6b63\u6574\u6570\u662f: {num}&quot;)<\/p>\n<p>            break  # \u8f93\u5165\u6709\u6548\uff0c\u9000\u51fa\u5faa\u73af<\/p>\n<p>        else:<\/p>\n<p>            print(&quot;\u8f93\u5165\u7684\u4e0d\u662f\u6b63\u6574\u6570\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\u3002&quot;)<\/p>\n<p>    except ValueError:<\/p>\n<p>        print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>while True<\/code>\u5faa\u73af\uff0c\u5b83\u4f1a\u4e0d\u65ad\u6267\u884c\uff0c\u76f4\u5230\u5728\u5faa\u73af\u5185\u90e8\u9047\u5230<code>break<\/code>\u8bed\u53e5\u3002<code>try-except<\/code>\u7ed3\u6784\u7528\u4e8e\u6355\u83b7\u7528\u6237\u8f93\u5165\u7684\u5f02\u5e38\u60c5\u51b5\uff08\u5982\u8f93\u5165\u975e\u6574\u6570\u65f6\u7684<code>ValueError<\/code>\uff09\uff0c\u4ece\u800c\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\uff0c\u5e76\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u4f7f\u7528for\u5faa\u73af<\/strong><\/p>\n<\/p>\n<p><p><code>for<\/code>\u5faa\u73af\u901a\u5e38\u7528\u4e8e\u904d\u5386\u5e8f\u5217\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u7b49\uff09\u3002\u867d\u7136\u5b83\u4e0d\u5982<code>while<\/code>\u5faa\u73af\u90a3\u6837\u76f4\u63a5\u7528\u4e8e\u91cd\u65b0\u5224\u65ad\u67d0\u4e2a\u6761\u4ef6\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4e5f\u53ef\u4ee5\u8fbe\u5230\u76f8\u4f3c\u7684\u6548\u679c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u68c0\u67e5\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\uff0c\u5982\u679c\u4e0d\u6ee1\u8db3\u5219\u91cd\u65b0\u5224\u65ad<\/p>\n<p>numbers = [1, 2, -3, 4, 5]<\/p>\n<p>for i, num in enumerate(numbers):<\/p>\n<p>    if num &lt; 0:<\/p>\n<p>        print(f&quot;\u53d1\u73b0\u8d1f\u6570: {num}\uff0c\u91cd\u65b0\u5224\u65ad...&quot;)<\/p>\n<p>        numbers[i] = abs(num)  # \u5c06\u8d1f\u6570\u8f6c\u6362\u4e3a\u6b63\u6570<\/p>\n<p>print(&quot;\u91cd\u65b0\u5224\u65ad\u540e\u7684\u5217\u8868:&quot;, numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u5728\u53d1\u73b0\u8d1f\u6570\u65f6\u8fdb\u884c\u76f8\u5e94\u5904\u7406\u3002\u867d\u7136\u8fd9\u4e0e\u524d\u9762\u7684\u793a\u4f8b\u4e0d\u5b8c\u5168\u76f8\u540c\uff0c\u4f46\u4e5f\u5c55\u793a\u4e86\u5982\u4f55\u5728\u5faa\u73af\u4e2d\u91cd\u65b0\u5224\u65ad\u548c\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e09\u3001\u9012\u5f52\u8c03\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u9012\u5f52\u8c03\u7528\u662f\u53e6\u4e00\u79cd\u8ba9\u8bed\u53e5\u91cd\u65b0\u5224\u65ad\u7684\u65b9\u6cd5\uff0c\u9012\u5f52\u51fd\u6570\u662f\u6307\u5728\u51fd\u6570\u5185\u90e8\u8c03\u7528\u81ea\u8eab\u7684\u51fd\u6570\u3002\u9012\u5f52\u8c03\u7528\u901a\u5e38\u7528\u4e8e\u89e3\u51b3\u5206\u800c\u6cbb\u4e4b\u7684\u95ee\u9898\uff0c\u4f8b\u5982\u8ba1\u7b97\u9636\u4e58\u3001\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u9012\u5f52\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u8ba1\u7b97\u9636\u4e58<\/p>\n<p>def factorial(n):<\/p>\n<p>    if n &lt; 0:<\/p>\n<p>        return &quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u975e\u8d1f\u6574\u6570\u3002&quot;<\/p>\n<p>    elif n == 0 or n == 1:<\/p>\n<p>        return 1<\/p>\n<p>    else:<\/p>\n<p>        return n * factorial(n - 1)<\/p>\n<p>num = int(input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u975e\u8d1f\u6574\u6570: &quot;))<\/p>\n<p>result = factorial(num)<\/p>\n<p>print(f&quot;{num} \u7684\u9636\u4e58\u662f: {result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u9012\u5f52\u51fd\u6570<code>factorial<\/code>\uff0c\u5b83\u901a\u8fc7\u8c03\u7528\u81ea\u8eab\u6765\u8ba1\u7b97\u9636\u4e58\u3002\u5982\u679c\u8f93\u5165\u7684\u662f\u8d1f\u6570\uff0c\u5219\u63d0\u793a\u8f93\u5165\u65e0\u6548\u3002<\/p>\n<\/p>\n<p><p><strong>\u56db\u3001\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u590d\u6742\u7684\u903b\u8f91\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06<code>while<\/code>\u5faa\u73af\u4e0e\u9012\u5f52\u8c03\u7528\u76f8\u7ed3\u5408\uff0c\u4ee5\u5b9e\u73b0\u66f4\u5f3a\u5927\u7684\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u548cwhile\u5faa\u73af\u9a8c\u8bc1\u7528\u6237\u8f93\u5165<\/p>\n<p>def get_positive_integer():<\/p>\n<p>    try:<\/p>\n<p>        num = int(input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570: &quot;))<\/p>\n<p>        if num &gt; 0:<\/p>\n<p>            return num<\/p>\n<p>        else:<\/p>\n<p>            print(&quot;\u8f93\u5165\u7684\u4e0d\u662f\u6b63\u6574\u6570\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\u3002&quot;)<\/p>\n<p>            return get_positive_integer()<\/p>\n<p>    except ValueError:<\/p>\n<p>        print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570\u3002&quot;)<\/p>\n<p>        return get_positive_integer()<\/p>\n<p>num = get_positive_integer()<\/p>\n<p>print(f&quot;\u60a8\u8f93\u5165\u7684\u6b63\u6574\u6570\u662f: {num}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u9012\u5f52\u51fd\u6570<code>get_positive_integer<\/code>\uff0c\u7528\u4e8e\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u6b63\u6574\u6570\u3002\u5982\u679c\u8f93\u5165\u65e0\u6548\u6216\u4e0d\u662f\u6b63\u6574\u6570\uff0c\u5219\u9012\u5f52\u8c03\u7528\u81ea\u8eab\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165\u6b63\u786e\u4e3a\u6b62\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0cPython \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u8ba9\u8bed\u53e5\u91cd\u65b0\u5224\u65ad\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u8bed\u53e5\u7684\u5faa\u73af\u5224\u65ad\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\uff08\u5982<code>while<\/code>\u6216<code>for<\/code>\u5faa\u73af\uff09\u6765\u5b9e\u73b0\u8bed\u53e5\u7684\u91cd\u65b0\u5224\u65ad\u3002<code>while<\/code>\u5faa\u73af\u4f1a\u5728\u6761\u4ef6\u4e3a\u771f\u65f6\u6301\u7eed\u6267\u884c\uff0c\u5141\u8bb8\u7a0b\u5e8f\u6839\u636e\u6761\u4ef6\u7684\u53d8\u5316\u91cd\u65b0\u5224\u65ad\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u7528\u6237\u8f93\u5165\u65f6\u4e0d\u65ad\u68c0\u67e5\u8f93\u5165\u7684\u6709\u6548\u6027\uff0c\u76f4\u5230\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u4e3a\u6b62\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u6761\u4ef6\u5224\u65ad\u66f4\u7075\u6d3b\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u548c\u5f02\u5e38\u5904\u7406\u6765\u63d0\u9ad8\u6761\u4ef6\u5224\u65ad\u7684\u7075\u6d3b\u6027\u3002\u4f7f\u7528\u51fd\u6570\u5c06\u5224\u65ad\u903b\u8f91\u5c01\u88c5\uff0c\u53ef\u4ee5\u91cd\u7528\u4ee3\u7801\u5e76\u4f7f\u6761\u4ef6\u66f4\u6613\u4e8e\u7ba1\u7406\u3002\u800c\u901a\u8fc7<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\uff0c\u53ef\u4ee5\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\uff0c\u786e\u4fdd\u7a0b\u5e8f\u5728\u9047\u5230\u5f02\u5e38\u65f6\u80fd\u591f\u7ee7\u7eed\u8fd0\u884c\u5e76\u91cd\u65b0\u8fdb\u884c\u5224\u65ad\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u7684\u6761\u4ef6\u8bed\u53e5\u5b9e\u73b0\u590d\u6742\u7684\u5224\u65ad\u903b\u8f91\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>if<\/code>\u3001<code>elif<\/code>\u548c<code>else<\/code>\u8bed\u53e5\u7ec4\u5408\u6765\u521b\u5efa\u590d\u6742\u7684\u5224\u65ad\u903b\u8f91\u3002\u901a\u8fc7\u5d4c\u5957\u8fd9\u4e9b\u6761\u4ef6\u8bed\u53e5\uff0c\u53ef\u4ee5\u6839\u636e\u591a\u4e2a\u6761\u4ef6\u7684\u7ec4\u5408\u6765\u6267\u884c\u4e0d\u540c\u7684\u4ee3\u7801\u5757\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u903b\u8f91\u8fd0\u7b97\u7b26\uff08\u5982<code>and<\/code>\u3001<code>or<\/code>\u548c<code>not<\/code>\uff09\u53ef\u4ee5\u8fdb\u4e00\u6b65\u589e\u5f3a\u6761\u4ef6\u5224\u65ad\u7684\u80fd\u529b\uff0c\u4f7f\u5f97\u6761\u4ef6\u5224\u65ad\u66f4\u52a0\u7cbe\u786e\u548c\u591a\u6837\u5316\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8ba9\u8bed\u53e5\u91cd\u65b0\u5224\u65ad\uff0c\u5982\u4f7f\u7528\u5faa\u73af\u3001\u9012\u5f52\u8c03\u7528\u3001\u5f02\u5e38\u5904\u7406\u7b49\u3002 \u5176\u4e2d\uff0c\u4f7f\u7528\u5faa\u73af\u662f\u6700\u5e38\u89c1\u548c [&hellip;]","protected":false},"author":3,"featured_media":1168634,"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\/1168627"}],"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=1168627"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1168627\/revisions"}],"predecessor-version":[{"id":1168635,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1168627\/revisions\/1168635"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1168634"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1168627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1168627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1168627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}