{"id":1139943,"date":"2025-01-08T22:15:14","date_gmt":"2025-01-08T14:15:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1139943.html"},"modified":"2025-01-08T22:15:17","modified_gmt":"2025-01-08T14:15:17","slug":"python%e5%a6%82%e4%bd%95%e4%bd%bf%e4%b8%80%e9%83%a8%e5%88%86%e4%bb%a3%e7%a0%81%e5%be%aa%e7%8e%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1139943.html","title":{"rendered":"python\u5982\u4f55\u4f7f\u4e00\u90e8\u5206\u4ee3\u7801\u5faa\u73af"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103056\/d76c4461-3a1c-4d3d-981b-d9261ab82080.webp\" alt=\"python\u5982\u4f55\u4f7f\u4e00\u90e8\u5206\u4ee3\u7801\u5faa\u73af\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u4f7f\u4e00\u90e8\u5206\u4ee3\u7801\u5faa\u73af\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u3001\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u3001\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u3001\u4f7f\u7528\u751f\u6210\u5668\u3002<\/strong>\u5176\u4e2d\uff0c<strong><code>for<\/code>\u5faa\u73af<\/strong>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u56e0\u4e3a\u5b83\u5728\u5904\u7406\u53ef\u8fed\u4ee3\u5bf9\u8c61\u65f6\u975e\u5e38\u9ad8\u6548\u548c\u7b80\u6d01\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u5c55\u5f00\u4ecb\u7ecd<code>for<\/code>\u5faa\u73af\u7684\u4f7f\u7528\u65b9\u6cd5\u53ca\u5176\u4f18\u52bf\u3002<\/p>\n<\/p>\n<p><p><strong><code>for<\/code>\u5faa\u73af<\/strong>\uff1a<code>for<\/code>\u5faa\u73af\u5728\u5904\u7406\u56fa\u5b9a\u6b21\u6570\u7684\u5faa\u73af\u65f6\u975e\u5e38\u65b9\u4fbf\uff0c\u5b83\u53ef\u4ee5\u8fed\u4ee3\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u3001\u5b57\u5178\u53ca\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002\u901a\u8fc7<code>for<\/code>\u5faa\u73af\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u63a7\u5236\u5faa\u73af\u6b21\u6570\u548c\u6761\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>for<\/code> \u5faa\u73af<\/h3>\n<\/p>\n<p><p><strong>1. \u57fa\u7840\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>for<\/code>\u5faa\u73af\u901a\u5e38\u7528\u4e8e\u904d\u5386\u4e00\u4e2a\u5e8f\u5217\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u6216\u5b57\u7b26\u4e32\uff09\u3002\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for element in sequence:<\/p>\n<p>    # \u6267\u884c\u7684\u4ee3\u7801\u5757<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u904d\u5386\u4e00\u4e2a\u5217\u8868\u5e76\u6253\u5370\u6bcf\u4e2a\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]<\/p>\n<p>for fruit in fruits:<\/p>\n<p>    print(fruit)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>apple<\/p>\n<p>banana<\/p>\n<p>cherry<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u904d\u5386\u5b57\u5178<\/strong><\/p>\n<\/p>\n<p><p>\u9664\u4e86\u5217\u8868\uff0c<code>for<\/code>\u5faa\u73af\u4e5f\u53ef\u4ee5\u7528\u6765\u904d\u5386\u5b57\u5178\u7684\u952e\u3001\u503c\u6216\u952e\u503c\u5bf9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">student_scores = {&quot;Alice&quot;: 85, &quot;Bob&quot;: 78, &quot;Charlie&quot;: 92}<\/p>\n<p>for student, score in student_scores.items():<\/p>\n<p>    print(f&quot;{student} scored {score}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Alice scored 85<\/p>\n<p>Bob scored 78<\/p>\n<p>Charlie scored 92<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. \u4f7f\u7528 <code>range()<\/code> \u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p><code>range()<\/code> \u51fd\u6570\u751f\u6210\u4e00\u4e2a\u6570\u503c\u5e8f\u5217\uff0c\u5e38\u7528\u4e8e\u9700\u8981\u6309\u7279\u5b9a\u6b21\u6570\u8fdb\u884c\u5faa\u73af\u7684\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(5):<\/p>\n<p>    print(f&quot;This is loop iteration {i}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>This is loop iteration 0<\/p>\n<p>This is loop iteration 1<\/p>\n<p>This is loop iteration 2<\/p>\n<p>This is loop iteration 3<\/p>\n<p>This is loop iteration 4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>while<\/code> \u5faa\u73af<\/h3>\n<\/p>\n<p><p><strong>1. \u57fa\u7840\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p><code>while<\/code>\u5faa\u73af\u7528\u4e8e\u5728\u6761\u4ef6\u4e3a\u771f\u7684\u60c5\u51b5\u4e0b\u53cd\u590d\u6267\u884c\u67d0\u6bb5\u4ee3\u7801\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while condition:<\/p>\n<p>    # \u6267\u884c\u7684\u4ee3\u7801\u5757<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u9636\u4e58\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 5<\/p>\n<p>factorial = 1<\/p>\n<p>while number &gt; 0:<\/p>\n<p>    factorial *= number<\/p>\n<p>    number -= 1<\/p>\n<p>print(f&quot;The factorial is {factorial}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>The factorial is 120<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u65e0\u9650\u5faa\u73af\u548c\u4e2d\u65ad<\/strong><\/p>\n<\/p>\n<p><p><code>while<\/code>\u5faa\u73af\u53ef\u4ee5\u521b\u5efa\u65e0\u9650\u5faa\u73af\uff0c\u901a\u5e38\u4e0e\u6761\u4ef6\u5224\u65ad\u548c<code>break<\/code>\u8bed\u53e5\u7ed3\u5408\u4f7f\u7528\u4ee5\u7ec8\u6b62\u5faa\u73af\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    user_input = input(&quot;Enter a number (or &#39;q&#39; to quit): &quot;)<\/p>\n<p>    if user_input == &#39;q&#39;:<\/p>\n<p>        break<\/p>\n<p>    print(f&quot;You entered {user_input}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u4e0d\u65ad\u8981\u6c42\u7528\u6237\u8f93\u5165\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165<code>&#39;q&#39;<\/code>\u4e3a\u6b62\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u9012\u5f52\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u662f\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u7684\u4e00\u79cd\u7f16\u7a0b\u6280\u5de7\uff0c\u9002\u7528\u4e8e\u67d0\u4e9b\u9700\u8981\u91cd\u590d\u64cd\u4f5c\u7684\u573a\u666f\u3002\u9012\u5f52\u7684\u57fa\u672c\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def recursive_function(params):<\/p>\n<p>    if base_case_condition:<\/p>\n<p>        return base_case_value<\/p>\n<p>    else:<\/p>\n<p>        return recursive_function(modified_params)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u8ba1\u7b97\u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def fibonacci(n):<\/p>\n<p>    if n &lt;= 1:<\/p>\n<p>        return n<\/p>\n<p>    else:<\/p>\n<p>        return fibonacci(n-1) + fibonacci(n-2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u901a\u8fc7\u9012\u5f52\u8ba1\u7b97\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u7b2c<code>n<\/code>\u9879\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u8fed\u4ee3\u5668\uff0c\u5141\u8bb8\u4f60\u5728\u8fed\u4ee3\u8fc7\u7a0b\u4e2d\u751f\u6210\u503c\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u751f\u6210\u6240\u6709\u503c\u3002\u751f\u6210\u5668\u901a\u8fc7<code>yield<\/code>\u8bed\u53e5\u751f\u6210\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def countdown(n):<\/p>\n<p>    while n &gt; 0:<\/p>\n<p>        yield n<\/p>\n<p>        n -= 1<\/p>\n<p>for number in countdown(5):<\/p>\n<p>    print(number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>5<\/p>\n<p>4<\/p>\n<p>3<\/p>\n<p>2<\/p>\n<p>1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4ee3\u7801\u4f18\u5316\u4e0e\u6027\u80fd\u8003\u91cf<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u5faa\u73af\u7ed3\u6784\u548c\u4f18\u5316\u4ee3\u7801\u6027\u80fd\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316\u548c\u6027\u80fd\u8003\u91cf\u7684\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<p><p><strong>1. \u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5faa\u73af\u5d4c\u5957<\/strong><\/p>\n<\/p>\n<p><p>\u5d4c\u5957\u5faa\u73af\u4f1a\u663e\u8457\u589e\u52a0\u4ee3\u7801\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u5e94\u5c3d\u91cf\u907f\u514d\u6216\u4f18\u5316\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5b57\u5178\u6216\u96c6\u5408\u6765\u51cf\u5c11\u67e5\u627e\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u4f7f\u7528\u751f\u6210\u5668\u8282\u7701\u5185\u5b58<\/strong><\/p>\n<\/p>\n<p><p>\u751f\u6210\u5668\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u4eec\u6309\u9700\u751f\u6210\u503c\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u5c06\u6240\u6709\u503c\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n<\/p>\n<p><p><strong>3. \u5229\u7528\u5185\u7f6e\u51fd\u6570\u548c\u5e93<\/strong><\/p>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684\u51fd\u6570\u548c\u5e93\u901a\u5e38\u7ecf\u8fc7\u9ad8\u5ea6\u4f18\u5316\uff0c\u5982\u4f7f\u7528<code>sum()<\/code>\u8ba1\u7b97\u5217\u8868\u5143\u7d20\u7684\u548c\uff0c\u6bd4\u4f7f\u7528\u5faa\u73af\u66f4\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p><strong>4. \u5c3d\u91cf\u51cf\u5c11\u5168\u5c40\u53d8\u91cf\u7684\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u5168\u5c40\u53d8\u91cf\u4f1a\u589e\u52a0\u4ee3\u7801\u7684\u8026\u5408\u5ea6\u548c\u590d\u6742\u5ea6\uff0c\u5e94\u5c3d\u91cf\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\u548c\u53c2\u6570\u4f20\u9012\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p><strong>1. \u6570\u636e\u5904\u7406\u548c\u5206\u6790<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u8fc7\u7a0b\u4e2d\uff0c\u5faa\u73af\u7ed3\u6784\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u6e05\u6d17\u3001\u8f6c\u6362\u548c\u805a\u5408\u3002\u4f8b\u5982\uff0c\u904d\u5386CSV\u6587\u4ef6\u7684\u884c\uff0c\u8ba1\u7b97\u6bcf\u4e2a\u7528\u6237\u7684\u603b\u6d88\u8d39\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>total_spent = {}<\/p>\n<p>with open(&#39;transactions.csv&#39;, &#39;r&#39;) as file:<\/p>\n<p>    reader = csv.DictReader(file)<\/p>\n<p>    for row in reader:<\/p>\n<p>        user = row[&#39;user&#39;]<\/p>\n<p>        amount = float(row[&#39;amount&#39;])<\/p>\n<p>        if user in total_spent:<\/p>\n<p>            total_spent[user] += amount<\/p>\n<p>        else:<\/p>\n<p>            total_spent[user] = amount<\/p>\n<p>print(total_spent)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u81ea\u52a8\u5316\u4efb\u52a1<\/strong><\/p>\n<\/p>\n<p><p>\u5faa\u73af\u7ed3\u6784\u5728\u81ea\u52a8\u5316\u811a\u672c\u4e2d\u975e\u5e38\u5e38\u89c1\uff0c\u4f8b\u5982\u5b9a\u671f\u68c0\u67e5\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u5e76\u5904\u7406\u65b0\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import time<\/p>\n<p>directory = &#39;\/path\/to\/directory&#39;<\/p>\n<p>processed_files = set()<\/p>\n<p>while True:<\/p>\n<p>    files = os.listdir(directory)<\/p>\n<p>    for file in files:<\/p>\n<p>        if file not in processed_files:<\/p>\n<p>            process_file(file)  # \u5047\u8bbe\u5b58\u5728\u4e00\u4e2a\u5904\u7406\u6587\u4ef6\u7684\u51fd\u6570<\/p>\n<p>            processed_files.add(file)<\/p>\n<p>    time.sleep(60)  # \u6bcf\u5206\u949f\u68c0\u67e5\u4e00\u6b21<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. Web\u5f00\u53d1\u548c\u722c\u866b<\/strong><\/p>\n<\/p>\n<p><p>\u5728Web\u5f00\u53d1\u548c\u722c\u866b\u4efb\u52a1\u4e2d\uff0c\u5faa\u73af\u7ed3\u6784\u7528\u4e8e\u904d\u5386\u7f51\u9875\u548c\u6570\u636e\uff0c\u4f8b\u5982\u6293\u53d6\u591a\u4e2a\u7f51\u9875\u7684\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>urls = [&#39;http:\/\/example.com\/page1&#39;, &#39;http:\/\/example.com\/page2&#39;]<\/p>\n<p>for url in urls:<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    soup = BeautifulSoup(response.content, &#39;html.parser&#39;)<\/p>\n<p>    print(soup.title.string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u6ce8\u610f\u4e8b\u9879\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p><strong>1. \u6ce8\u610f\u5faa\u73af\u6761\u4ef6\u548c\u7ec8\u6b62\u6761\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u786e\u4fdd\u5faa\u73af\u6709\u660e\u786e\u7684\u7ec8\u6b62\u6761\u4ef6\uff0c\u907f\u514d\u8fdb\u5165\u65e0\u9650\u5faa\u73af\u3002\u4f8b\u5982\uff0c\u5728<code>while<\/code>\u5faa\u73af\u4e2d\u8bbe\u7f6e\u9002\u5f53\u7684\u6761\u4ef6\u68c0\u67e5\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u907f\u514d\u4fee\u6539\u6b63\u5728\u904d\u5386\u7684\u5e8f\u5217<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u904d\u5386\u5e8f\u5217\u65f6\u4fee\u6539\u5b83\u53ef\u80fd\u4f1a\u5bfc\u81f4\u610f\u5916\u7ed3\u679c\uff0c\u5efa\u8bae\u521b\u5efa\u526f\u672c\u6216\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>3. \u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u9ad8\u6548\u7684\u521b\u5efa\u5217\u8868\u548c\u751f\u6210\u5668\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">squares = [x2 for x in range(10)]<\/p>\n<p>print(squares)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>4. \u5229\u7528\u5e76\u884c\u548c\u5f02\u6b65\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u8017\u65f6\u4efb\u52a1\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5e76\u884c\u5904\u7406\uff08\u5982\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\uff09\u548c\u5f02\u6b65\u5904\u7406\u6765\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7684\u6765\u8bf4\uff0c<strong>\u9009\u62e9\u5408\u9002\u7684\u5faa\u73af\u7ed3\u6784\u548c\u4f18\u5316\u4ee3\u7801\u6027\u80fd\u662f\u7f16\u5199\u9ad8\u6548Python\u4ee3\u7801\u7684\u5173\u952e<\/strong>\u3002\u901a\u8fc7\u6df1\u5165\u7406\u89e3<code>for<\/code>\u5faa\u73af\u3001<code>while<\/code>\u5faa\u73af\u3001\u9012\u5f52\u51fd\u6570\u548c\u751f\u6210\u5668\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u5404\u79cd\u7f16\u7a0b\u573a\u666f\u4e2d\u7075\u6d3b\u5e94\u7528\u5faa\u73af\u7ed3\u6784\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u8fd0\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u4ee3\u7801\u7684\u5faa\u73af\u7ed3\u6784\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u548c<code>while<\/code>\u5faa\u73af\u6765\u91cd\u590d\u6267\u884c\u4e00\u90e8\u5206\u4ee3\u7801\u3002<code>for<\/code>\u5faa\u73af\u9002\u5408\u4e8e\u5df2\u77e5\u6b21\u6570\u7684\u5faa\u73af\uff0c\u800c<code>while<\/code>\u5faa\u73af\u9002\u5408\u4e8e\u57fa\u4e8e\u6761\u4ef6\u7684\u5faa\u73af\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\"># \u4f7f\u7528for\u5faa\u73af\nfor i in range(5):\n    print(&quot;\u8fd9\u662f\u7b2c&quot;, i + 1, &quot;\u6b21\u5faa\u73af&quot;)\n\n# \u4f7f\u7528while\u5faa\u73af\ncount = 0\nwhile count &lt; 5:\n    print(&quot;\u8fd9\u662f\u7b2c&quot;, count + 1, &quot;\u6b21\u5faa\u73af&quot;)\n    count += 1\n<\/code><\/pre>\n<p><strong>\u5728\u5faa\u73af\u4e2d\u5982\u4f55\u63a7\u5236\u4ee3\u7801\u7684\u6267\u884c\u6b21\u6570\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u5faa\u73af\u7684\u6761\u4ef6\u6216\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u6765\u63a7\u5236\u5faa\u73af\u7684\u6267\u884c\u6b21\u6570\u3002\u4f8b\u5982\uff0c\u7ed3\u5408<code>for<\/code>\u548c<code>if<\/code>\u6761\u4ef6\uff0c\u53ef\u4ee5\u5728\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\u63d0\u524d\u9000\u51fa\u5faa\u73af\uff1a  <\/p>\n<pre><code class=\"language-python\">for i in range(10):\n    if i == 5:\n        break  # \u5f53i\u7b49\u4e8e5\u65f6\u9000\u51fa\u5faa\u73af\n    print(i)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728\u5faa\u73af\u4e2d\u5904\u7406\u7528\u6237\u8f93\u5165\uff1f<\/strong><br \/>\u5728\u5faa\u73af\u4e2d\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u7528\u6237\u8f93\u5165\uff0c\u5e76\u6839\u636e\u8f93\u5165\u7684\u5185\u5bb9\u51b3\u5b9a\u662f\u5426\u7ee7\u7eed\u5faa\u73af\u3002\u4f8b\u5982\uff0c\u7528\u6237\u8f93\u5165\u7279\u5b9a\u5b57\u7b26\u65f6\u53ef\u4ee5\u7ec8\u6b62\u5faa\u73af\uff1a  <\/p>\n<pre><code class=\"language-python\">while True:\n    user_input = input(&quot;\u8bf7\u8f93\u5165\u5185\u5bb9\uff08\u8f93\u5165&#39;\u9000\u51fa&#39;\u7ed3\u675f\u5faa\u73af\uff09\uff1a&quot;)\n    if user_input == &#39;\u9000\u51fa&#39;:\n        break\n    print(&quot;\u60a8\u8f93\u5165\u7684\u5185\u5bb9\u662f\uff1a&quot;, user_input)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u5f97\u7a0b\u5e8f\u66f4\u52a0\u4e92\u52a8\uff0c\u540c\u65f6\u7528\u6237\u53ef\u4ee5\u6839\u636e\u9700\u8981\u51b3\u5b9a\u5faa\u73af\u7684\u7ed3\u675f\u65f6\u673a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4f7f\u4e00\u90e8\u5206\u4ee3\u7801\u5faa\u73af\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528for\u5faa\u73af\u3001\u4f7f\u7528while\u5faa\u73af\u3001\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u3001\u4f7f\u7528\u751f\u6210\u5668\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1139953,"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\/1139943"}],"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=1139943"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1139943\/revisions"}],"predecessor-version":[{"id":1139954,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1139943\/revisions\/1139954"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1139953"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1139943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1139943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1139943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}