{"id":204587,"date":"2025-05-29T09:40:44","date_gmt":"2025-05-29T01:40:44","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204587\/"},"modified":"2025-05-29T09:40:44","modified_gmt":"2025-05-29T01:40:44","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-python-%e7%9a%84-while-%e5%be%aa%e7%8e%af%e5%88%a4%e6%96%ad%e4%b8%80%e4%b8%aa%e6%95%b0%e6%98%af%e5%90%a6%e4%b8%ba%e8%b4%a8%e6%95%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204587\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Python \u7684 while \u5faa\u73af\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u8d28\u6570?"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 Python \u7684 while \u5faa\u73af\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u8d28\u6570?<\/h1>\n<p>\u4f60\u5728\u5b66\u4e60\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528 Python \u7684 while \u5faa\u73af\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u8d28\u6570?\u300b<\/span>\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241111\/173129782867318224d8fbd.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python \u4f7f\u7528 while \u5faa\u73af\u5224\u65ad\u8d28\u6570<\/strong><\/p>\n<p>\u5bf9\u4e8e python \u521d\u5b66\u8005\u6765\u8bf4\uff0c\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u8d28\u6570\u662f\u4e00\u9879\u5e38\u89c1\u4efb\u52a1\u3002\u9664\u4e86\u4f7f\u7528 for \u5faa\u73af\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528 while \u5faa\u73af\u6765\u5b9e\u73b0\u6b64\u76ee\u7684\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 while \u5faa\u73af\u5224\u65ad\u8d28\u6570\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>num = eval(input(\"\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57:\"))\n\nif num &lt;= 1:\n    print(\"\u8fd9\u4e0d\u662f\u8d28\u6570\")\nelif num == 2:\n    print(\"\u8fd9\u662f\u4e00\u4e2a\u8d28\u6570!\")\nelse:\n    i = 2\n    while i &lt; num:\n        if num % i == 0:\n            print(\"\u8fd9\u4e0d\u662f\u4e00\u4e2a\u8d28\u6570\")\n            break\n        i += 1\n    else:\n        print(\"\u8fd9\u662f\u4e00\u4e2a\u8d28\u6570!\")<\/pre>\n<p>\u8be5\u4ee3\u7801\u9996\u5148\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u6570\u5b57\u3002\u5982\u679c\u6570\u5b57\u5c0f\u4e8e\u6216\u7b49\u4e8e 1\uff0c\u5219\u5b83\u4e0d\u662f\u8d28\u6570\u3002\u5982\u679c\u6570\u5b57\u4e3a 2\uff0c\u5219\u5b83\u662f\u4e00\u4e2a\u8d28\u6570\u3002\u5bf9\u4e8e\u5176\u4ed6\u6570\u5b57\uff0c\u4ee3\u7801\u4f7f\u7528 while \u5faa\u73af\u5728 2 \u5230\u6570\u5b57\u4e4b\u95f4\uff08\u4e0d\u5305\u62ec\u6570\u5b57\u672c\u8eab\uff09\u7684\u8303\u56f4\u5185\u9010\u4e00\u68c0\u67e5\u5b83\u662f\u5426\u53ef\u4ee5\u88ab\u6574\u9664\u3002\u5982\u679c\u5b83\u53ef\u4ee5\u88ab\u6574\u9664\uff0c\u5219\u4e0d\u662f\u8d28\u6570\u3002\u5426\u5219\uff0c\u5b83\u662f\u4e00\u4e2a\u8d28\u6570\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 Python \u7684 while \u5faa\u73af\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u8d28\u6570?\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 Python \u7684 wh&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204587","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204587","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204587"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204587\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}