{"id":1083242,"date":"2025-01-08T12:56:15","date_gmt":"2025-01-08T04:56:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1083242.html"},"modified":"2025-01-08T12:56:19","modified_gmt":"2025-01-08T04:56:19","slug":"python%e5%a6%82%e4%bd%95%e8%a6%81%e6%b1%82%e7%94%a8%e6%88%b7%e8%be%93%e5%85%a5n%e4%b8%aa%e6%95%b4%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1083242.html","title":{"rendered":"python\u5982\u4f55\u8981\u6c42\u7528\u6237\u8f93\u5165n\u4e2a\u6574\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24193918\/f44f2a84-8000-430a-8162-2cd16316de42.webp\" alt=\"python\u5982\u4f55\u8981\u6c42\u7528\u6237\u8f93\u5165n\u4e2a\u6574\u6570\" \/><\/p>\n<p><p> <strong>Python \u8981\u6c42\u7528\u6237\u8f93\u5165 n \u4e2a\u6574\u6570\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u3001\u4f7f\u7528 <code>while<\/code> \u5faa\u73af\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u6765\u9010\u4e00\u83b7\u53d6\u7528\u6237\u8f93\u5165\u5e76\u5b58\u50a8\u5230\u5217\u8868\u4e2d\u3002\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u76f4\u89c2\uff0c\u800c\u4e14\u4fbf\u4e8e\u540e\u7eed\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u6765\u8981\u6c42\u7528\u6237\u8f93\u5165 n \u4e2a\u6574\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8981\u6c42\u7528\u6237\u8f93\u5165\u6574\u6570\u7684\u6570\u91cf<\/p>\n<p>n = int(input(&quot;\u8bf7\u8f93\u5165\u8981\u8f93\u5165\u7684\u6574\u6570\u6570\u91cf: &quot;))<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868\u6765\u5b58\u50a8\u8f93\u5165\u7684\u6574\u6570<\/strong><\/h2>\n<p>integer_list = []<\/p>\n<h2><strong>\u4f7f\u7528 for \u5faa\u73af\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u6574\u6570<\/strong><\/h2>\n<p>for i in range(n):<\/p>\n<p>    integer = int(input(f&quot;\u8bf7\u8f93\u5165\u7b2c {i+1} \u4e2a\u6574\u6570: &quot;))<\/p>\n<p>    integer_list.append(integer)<\/p>\n<h2><strong>\u8f93\u51fa\u7528\u6237\u8f93\u5165\u7684\u6574\u6570\u5217\u8868<\/strong><\/h2>\n<p>print(&quot;\u8f93\u5165\u7684\u6574\u6570\u5217\u8868\u4e3a:&quot;, integer_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u901a\u8fc7 <code>input<\/code> \u51fd\u6570\u83b7\u53d6\u7528\u6237\u5e0c\u671b\u8f93\u5165\u7684\u6574\u6570\u6570\u91cf <code>n<\/code>\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u7c7b\u578b\u3002\u7136\u540e\uff0c\u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868 <code>integer_list<\/code> \u6765\u5b58\u50a8\u7528\u6237\u8f93\u5165\u7684\u6574\u6570\u3002\u63a5\u4e0b\u6765\uff0c\u901a\u8fc7 <code>for<\/code> \u5faa\u73af\uff0c\u9010\u4e00\u83b7\u53d6\u7528\u6237\u7684\u8f93\u5165\uff0c\u5e76\u5c06\u6bcf\u4e2a\u8f93\u5165\u7684\u6574\u6570\u6dfb\u52a0\u5230 <code>integer_list<\/code> \u4e2d\u3002\u6700\u540e\uff0c\u8f93\u51fa\u5b58\u50a8\u7684\u6574\u6570\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8fdb\u4e00\u6b65\u63a2\u8ba8\u5176\u4ed6\u65b9\u6cd5\u53ca\u4e00\u4e9b\u76f8\u5173\u7684\u9ad8\u7ea7\u6280\u5de7\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>while<\/code> \u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u9664\u4e86 <code>for<\/code> \u5faa\u73af\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528 <code>while<\/code> \u5faa\u73af\u6765\u5b9e\u73b0\u540c\u6837\u7684\u529f\u80fd\u3002\u4f7f\u7528 <code>while<\/code> \u5faa\u73af\u7684\u597d\u5904\u662f\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u63a7\u5236\u5faa\u73af\u7684\u7ec8\u6b62\u6761\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8981\u6c42\u7528\u6237\u8f93\u5165\u6574\u6570\u7684\u6570\u91cf<\/p>\n<p>n = int(input(&quot;\u8bf7\u8f93\u5165\u8981\u8f93\u5165\u7684\u6574\u6570\u6570\u91cf: &quot;))<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868\u6765\u5b58\u50a8\u8f93\u5165\u7684\u6574\u6570<\/strong><\/h2>\n<p>integer_list = []<\/p>\n<h2><strong>\u4f7f\u7528 while \u5faa\u73af\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u6574\u6570<\/strong><\/h2>\n<p>count = 0<\/p>\n<p>while count &lt; n:<\/p>\n<p>    integer = int(input(f&quot;\u8bf7\u8f93\u5165\u7b2c {count+1} \u4e2a\u6574\u6570: &quot;))<\/p>\n<p>    integer_list.append(integer)<\/p>\n<p>    count += 1<\/p>\n<h2><strong>\u8f93\u51fa\u7528\u6237\u8f93\u5165\u7684\u6574\u6570\u5217\u8868<\/strong><\/h2>\n<p>print(&quot;\u8f93\u5165\u7684\u6574\u6570\u5217\u8868\u4e3a:&quot;, integer_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>while<\/code> \u5faa\u73af\u6765\u9010\u4e00\u83b7\u53d6\u7528\u6237\u7684\u8f93\u5165\uff0c\u5e76\u901a\u8fc7\u8ba1\u6570\u5668 <code>count<\/code> \u6765\u63a7\u5236\u5faa\u73af\u7684\u6b21\u6570\u3002\u6bcf\u6b21\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u540e\uff0c\u8ba1\u6570\u5668 <code>count<\/code> \u52a0 1\uff0c\u76f4\u5230 <code>count<\/code> \u7b49\u4e8e <code>n<\/code> \u65f6\uff0c\u5faa\u73af\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u7528\u6237\u7684\u8f93\u5165\u5f62\u5f0f\u662f\u4ee5\u7a7a\u683c\u6216\u9017\u53f7\u5206\u9694\u7684\u4e00\u884c\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\u6765\u7b80\u5316\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8981\u6c42\u7528\u6237\u8f93\u5165\u6574\u6570\u7684\u6570\u91cf<\/p>\n<p>n = int(input(&quot;\u8bf7\u8f93\u5165\u8981\u8f93\u5165\u7684\u6574\u6570\u6570\u91cf: &quot;))<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u89e3\u6790\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u6574\u6570<\/strong><\/h2>\n<p>integer_list = [int(x) for x in input(f&quot;\u8bf7\u8f93\u5165 {n} \u4e2a\u6574\u6570\uff08\u7528\u7a7a\u683c\u5206\u9694\uff09: &quot;).split()]<\/p>\n<h2><strong>\u8f93\u51fa\u7528\u6237\u8f93\u5165\u7684\u6574\u6570\u5217\u8868<\/strong><\/h2>\n<p>print(&quot;\u8f93\u5165\u7684\u6574\u6570\u5217\u8868\u4e3a:&quot;, integer_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u83b7\u53d6\u7528\u6237\u5e0c\u671b\u8f93\u5165\u7684\u6574\u6570\u6570\u91cf <code>n<\/code>\uff0c\u7136\u540e\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u884c\u6574\u6570\uff0c\u5e76\u4f7f\u7528 <code>split<\/code> \u65b9\u6cd5\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u6309\u7a7a\u683c\u5206\u9694\u6210\u591a\u4e2a\u5b50\u5b57\u7b26\u4e32\uff0c\u518d\u901a\u8fc7\u5217\u8868\u89e3\u6790\u5c06\u6bcf\u4e2a\u5b50\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u5e76\u5b58\u50a8\u5230 <code>integer_list<\/code> \u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6570\u636e\u9a8c\u8bc1\u548c\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4fdd\u8bc1\u7528\u6237\u8f93\u5165\u7684\u6709\u6548\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e9b\u6570\u636e\u9a8c\u8bc1\u548c\u5f02\u5e38\u5904\u7406\u673a\u5236\u3002\u8fd9\u6837\u53ef\u4ee5\u9632\u6b62\u7528\u6237\u8f93\u5165\u975e\u6cd5\u6570\u636e\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8981\u6c42\u7528\u6237\u8f93\u5165\u6574\u6570\u7684\u6570\u91cf<\/p>\n<p>n = int(input(&quot;\u8bf7\u8f93\u5165\u8981\u8f93\u5165\u7684\u6574\u6570\u6570\u91cf: &quot;))<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868\u6765\u5b58\u50a8\u8f93\u5165\u7684\u6574\u6570<\/strong><\/h2>\n<p>integer_list = []<\/p>\n<h2><strong>\u4f7f\u7528 for \u5faa\u73af\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u6574\u6570\uff0c\u5e76\u6dfb\u52a0\u6570\u636e\u9a8c\u8bc1\u548c\u5f02\u5e38\u5904\u7406<\/strong><\/h2>\n<p>for i in range(n):<\/p>\n<p>    while True:<\/p>\n<p>        try:<\/p>\n<p>            integer = int(input(f&quot;\u8bf7\u8f93\u5165\u7b2c {i+1} \u4e2a\u6574\u6570: &quot;))<\/p>\n<p>            integer_list.append(integer)<\/p>\n<p>            break<\/p>\n<p>        except ValueError:<\/p>\n<p>            print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u3002&quot;)<\/p>\n<h2><strong>\u8f93\u51fa\u7528\u6237\u8f93\u5165\u7684\u6574\u6570\u5217\u8868<\/strong><\/h2>\n<p>print(&quot;\u8f93\u5165\u7684\u6574\u6570\u5217\u8868\u4e3a:&quot;, integer_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728 <code>for<\/code> \u5faa\u73af\u5185\u4f7f\u7528\u4e86\u4e00\u4e2a <code>while<\/code> \u5faa\u73af\u6765\u53cd\u590d\u8981\u6c42\u7528\u6237\u8f93\u5165\u6574\u6570\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6709\u6548\u7684\u6574\u6570\u4e3a\u6b62\u3002\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u65e0\u6cd5\u8f6c\u6362\u4e3a\u6574\u6570\uff08\u4f8b\u5982\u8f93\u5165\u4e86\u5b57\u6bcd\u6216\u7b26\u53f7\uff09\uff0c\u7a0b\u5e8f\u4f1a\u6355\u6349\u5230 <code>ValueError<\/code> \u5f02\u5e38\uff0c\u5e76\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u51fd\u6570\u5c01\u88c5<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u590d\u7528\u6027\u548c\u53ef\u8bfb\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e0a\u8ff0\u529f\u80fd\u5c01\u88c5\u5230\u4e00\u4e2a\u51fd\u6570\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_integers(n):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684 n \u4e2a\u6574\u6570<\/p>\n<p>    :param n: \u6574\u6570\u6570\u91cf<\/p>\n<p>    :return: \u5305\u542b n \u4e2a\u6574\u6570\u7684\u5217\u8868<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    integer_list = []<\/p>\n<p>    for i in range(n):<\/p>\n<p>        while True:<\/p>\n<p>            try:<\/p>\n<p>                integer = int(input(f&quot;\u8bf7\u8f93\u5165\u7b2c {i+1} \u4e2a\u6574\u6570: &quot;))<\/p>\n<p>                integer_list.append(integer)<\/p>\n<p>                break<\/p>\n<p>            except ValueError:<\/p>\n<p>                print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u3002&quot;)<\/p>\n<p>    return integer_list<\/p>\n<h2><strong>\u8981\u6c42\u7528\u6237\u8f93\u5165\u6574\u6570\u7684\u6570\u91cf<\/strong><\/h2>\n<p>n = int(input(&quot;\u8bf7\u8f93\u5165\u8981\u8f93\u5165\u7684\u6574\u6570\u6570\u91cf: &quot;))<\/p>\n<h2><strong>\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u6574\u6570\u5217\u8868<\/strong><\/h2>\n<p>integer_list = get_integers(n)<\/p>\n<h2><strong>\u8f93\u51fa\u7528\u6237\u8f93\u5165\u7684\u6574\u6570\u5217\u8868<\/strong><\/h2>\n<p>print(&quot;\u8f93\u5165\u7684\u6574\u6570\u5217\u8868\u4e3a:&quot;, integer_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a <code>get_integers<\/code> \u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570 <code>n<\/code>\uff0c\u8868\u793a\u7528\u6237\u5e0c\u671b\u8f93\u5165\u7684\u6574\u6570\u6570\u91cf\u3002\u51fd\u6570\u5185\u90e8\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u548c\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u6574\u6570\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5230\u5217\u8868\u4e2d\u3002\u6700\u540e\uff0c\u51fd\u6570\u8fd4\u56de\u5305\u542b\u7528\u6237\u8f93\u5165\u7684\u6574\u6570\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u591a\u79cd\u8981\u6c42\u7528\u6237\u8f93\u5165 n \u4e2a\u6574\u6570\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u3001<code>while<\/code> \u5faa\u73af\u3001\u5217\u8868\u89e3\u6790\uff0c\u4ee5\u53ca\u6570\u636e\u9a8c\u8bc1\u548c\u5f02\u5e38\u5904\u7406\u3002\u6211\u4eec\u8fd8\u5c06\u8fd9\u4e9b\u529f\u80fd\u5c01\u88c5\u5230\u4e00\u4e2a\u51fd\u6570\u4e2d\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u590d\u7528\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><p>\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u540e\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u5f0f\u6765\u83b7\u53d6\u7528\u6237\u8f93\u5165\uff0c\u5e76\u786e\u4fdd\u8f93\u5165\u6570\u636e\u7684\u6709\u6548\u6027\u548c\u5b89\u5168\u6027\u3002\u8fd9\u4e9b\u6280\u5de7\u4e0d\u4ec5\u9002\u7528\u4e8e\u8981\u6c42\u7528\u6237\u8f93\u5165\u6574\u6570\uff0c\u8fd8\u53ef\u4ee5\u6269\u5c55\u5230\u5176\u4ed6\u7c7b\u578b\u7684\u6570\u636e\u8f93\u5165\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6709\u6548\u5730\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u6574\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u7528\u6237\u7684\u8f93\u5165\u3002\u8981\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u662f\u6574\u6570\uff0c\u53ef\u4ee5\u7ed3\u5408<code>int()<\/code>\u51fd\u6570\u548c\u5f02\u5e38\u5904\u7406\u6765\u9a8c\u8bc1\u8f93\u5165\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u4e00\u4e2a\u5faa\u73af\u4e2d\u53cd\u590d\u63d0\u793a\u7528\u6237\u8f93\u5165\uff0c\u76f4\u5230\u6210\u529f\u83b7\u53d6\u6240\u9700\u7684\u6574\u6570\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u4ec0\u4e48\u65b9\u6cd5\u6765\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u662f\u4e00\u4e2a\u6709\u6548\u7684\u6574\u6570\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u7528\u6237\u8f93\u5165\u4e2d\u7684\u9519\u8bef\u3002\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u503c\u65e0\u6cd5\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">while True:\n    try:\n        number = int(input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570: &quot;))\n        break\n    except ValueError:\n        print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u3002&quot;)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4e00\u6b21\u6027\u83b7\u53d6\u591a\u4e2a\u6574\u6570\uff1f<\/strong><br \/>\u4e3a\u4e86\u83b7\u53d6\u591a\u4e2a\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u91cd\u590d\u63d0\u793a\u7528\u6237\u8f93\u5165\uff0c\u6216\u8005\u8981\u6c42\u7528\u6237\u5728\u4e00\u884c\u4e2d\u8f93\u5165\u591a\u4e2a\u503c\uff0c\u5e76\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\u5c06\u5176\u5206\u9694\u5f00\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">numbers = input(&quot;\u8bf7\u8f93\u5165\u591a\u4e2a\u6574\u6570\uff0c\u4ee5\u7a7a\u683c\u5206\u9694: &quot;).split()\nintegers = [int(num) for num in numbers]\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u5c06\u7528\u6237\u8f93\u5165\u7684\u6240\u6709\u6574\u6570\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u65b9\u4fbf\u540e\u7eed\u5904\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u6574\u6570\u4ee5\u8fdb\u884c\u8ba1\u7b97\u6216\u5176\u4ed6\u64cd\u4f5c\uff1f<\/strong><br \/>\u4e00\u65e6\u6536\u96c6\u5230\u7528\u6237\u8f93\u5165\u7684\u6574\u6570\uff0c\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u5982\u6c42\u548c\u3001\u5e73\u5747\u503c\u3001\u67e5\u627e\u6700\u5927\u6700\u5c0f\u503c\u7b49\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>sum()<\/code>\u548c<code>len()<\/code>\u53ef\u4ee5\u8f7b\u677e\u8ba1\u7b97\u5e73\u5747\u503c\uff1a<\/p>\n<pre><code class=\"language-python\">average = sum(integers) \/ len(integers)\nprint(&quot;\u5e73\u5747\u503c\u4e3a:&quot;, average)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u8981\u6c42\u7528\u6237\u8f93\u5165 n \u4e2a\u6574\u6570\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 for \u5faa\u73af\u3001\u4f7f\u7528 while \u5faa\u73af\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1083252,"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\/1083242"}],"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=1083242"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1083242\/revisions"}],"predecessor-version":[{"id":1083254,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1083242\/revisions\/1083254"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1083252"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1083242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1083242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1083242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}