{"id":1049041,"date":"2024-12-31T13:56:11","date_gmt":"2024-12-31T05:56:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1049041.html"},"modified":"2024-12-31T13:56:13","modified_gmt":"2024-12-31T05:56:13","slug":"python%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8%e5%be%aa%e7%8e%af%e5%90%8c%e6%97%b6%e8%be%93%e5%85%a5%e5%a4%9a%e4%b8%aa%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1049041.html","title":{"rendered":"python\u5982\u4f55\u5229\u7528\u5faa\u73af\u540c\u65f6\u8f93\u5165\u591a\u4e2a\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/c97647fd-ec4b-4c4b-966c-7aeaf027ca15.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5229\u7528\u5faa\u73af\u540c\u65f6\u8f93\u5165\u591a\u4e2a\u6570\" \/><\/p>\n<p><p> <strong>Python\u5229\u7528\u5faa\u73af\u540c\u65f6\u8f93\u5165\u591a\u4e2a\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u3001<code>while<\/code>\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u65b9\u6cd5\u6765\u5b9e\u73b0<\/strong>\u3001<strong>\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u7b80\u6d01\u3001\u66f4\u9ad8\u6548<\/strong>\u3001<strong>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u5c24\u4e3a\u91cd\u8981<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u7ed9\u51fa\u4e00\u4e9b\u5b9e\u9645\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u8f93\u5165\u591a\u4e2a\u6570<\/h3>\n<\/p>\n<p><p><code>for<\/code> \u5faa\u73af\u662fPython\u4e2d\u5e38\u89c1\u7684\u5faa\u73af\u7ed3\u6784\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b83\u6765\u5b9e\u73b0\u591a\u6b21\u8f93\u5165\u3002\u5047\u8bbe\u6211\u4eec\u9700\u8981\u8f93\u51655\u4e2a\u6574\u6570\uff0c\u5e76\u5c06\u5b83\u4eec\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = []<\/p>\n<p>for _ in range(5):<\/p>\n<p>    num = int(input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570: &quot;))<\/p>\n<p>    numbers.append(num)<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6574\u6570\u5217\u8868:&quot;, numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a<code>for<\/code>\u5faa\u73af\uff0c\u5b83\u5c06\u6267\u884c5\u6b21\uff0c\u6bcf\u6b21\u5faa\u73af\u65f6\u90fd\u4f1a\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6574\u6570\uff0c\u5e76\u5c06\u8f93\u5165\u7684\u6574\u6570\u6dfb\u52a0\u5230<code>numbers<\/code>\u5217\u8868\u4e2d\u3002\u5faa\u73af\u7ed3\u675f\u540e\uff0c\u5217\u8868\u4e2d\u5c31\u5305\u542b\u4e86\u7528\u6237\u8f93\u5165\u7684\u6240\u6709\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>while<\/code> \u5faa\u73af\u8f93\u5165\u591a\u4e2a\u6570<\/h3>\n<\/p>\n<p><p><code>while<\/code> \u5faa\u73af\u4e5f\u662fPython\u4e2d\u5e38\u89c1\u7684\u5faa\u73af\u7ed3\u6784\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b83\u6765\u5b9e\u73b0\u591a\u6b21\u8f93\u5165\u3002\u5047\u8bbe\u6211\u4eec\u9700\u8981\u8f93\u5165\u4e00\u7cfb\u5217\u6574\u6570\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u7279\u5b9a\u7684\u503c\uff08\u4f8b\u5982-1\uff09\u65f6\u505c\u6b62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = []<\/p>\n<p>while True:<\/p>\n<p>    num = int(input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570\uff08\u8f93\u5165-1\u505c\u6b62\uff09: &quot;))<\/p>\n<p>    if num == -1:<\/p>\n<p>        break<\/p>\n<p>    numbers.append(num)<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6574\u6570\u5217\u8868:&quot;, numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a<code>while<\/code>\u5faa\u73af\uff0c\u5b83\u5c06\u4e0d\u65ad\u6267\u884c\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165-1\u4e3a\u6b62\u3002\u6bcf\u6b21\u5faa\u73af\u65f6\u90fd\u4f1a\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6574\u6570\uff0c\u5e76\u5c06\u8f93\u5165\u7684\u6574\u6570\u6dfb\u52a0\u5230<code>numbers<\/code>\u5217\u8868\u4e2d\u3002\u5faa\u73af\u7ed3\u675f\u540e\uff0c\u5217\u8868\u4e2d\u5c31\u5305\u542b\u4e86\u7528\u6237\u8f93\u5165\u7684\u6240\u6709\u6574\u6570\uff08\u4e0d\u5305\u62ec-1\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8f93\u5165\u591a\u4e2a\u6570<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u521b\u5efa\u5217\u8868\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b83\u6765\u5b9e\u73b0\u591a\u6b21\u8f93\u5165\u3002\u5047\u8bbe\u6211\u4eec\u9700\u8981\u8f93\u51655\u4e2a\u6574\u6570\uff0c\u5e76\u5c06\u5b83\u4eec\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [int(input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570: &quot;)) for _ in range(5)]<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6574\u6570\u5217\u8868:&quot;, numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u5b83\u5c06\u6267\u884c5\u6b21\uff0c\u6bcf\u6b21\u5faa\u73af\u65f6\u90fd\u4f1a\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6574\u6570\uff0c\u5e76\u5c06\u8f93\u5165\u7684\u6574\u6570\u76f4\u63a5\u5b58\u50a8\u5728<code>numbers<\/code>\u5217\u8868\u4e2d\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u4f7f\u4ee3\u7801\u66f4\u7b80\u6d01\uff0c\u4f46\u5728\u5904\u7406\u8f83\u590d\u6742\u7684\u8f93\u5165\u903b\u8f91\u65f6\uff0c\u53ef\u80fd\u4e0d\u5982<code>for<\/code>\u5faa\u73af\u548c<code>while<\/code>\u5faa\u73af\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4ece\u4e00\u884c\u8f93\u5165\u591a\u4e2a\u6570<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u5e0c\u671b\u4ece\u4e00\u884c\u4e2d\u8f93\u5165\u591a\u4e2a\u6570\uff0c\u5e76\u5c06\u5b83\u4eec\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>split<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = list(map(int, input(&quot;\u8bf7\u8f93\u5165\u591a\u4e2a\u6574\u6570\uff0c\u7528\u7a7a\u683c\u5206\u9694: &quot;).split()))<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6574\u6570\u5217\u8868:&quot;, numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7528\u6237\u5c06\u6240\u6709\u6574\u6570\u8f93\u5165\u5230\u4e00\u884c\u4e2d\uff0c\u5e76\u7528\u7a7a\u683c\u5206\u9694\u3002<code>input<\/code>\u51fd\u6570\u8bfb\u53d6\u6574\u884c\u8f93\u5165\uff0c<code>split<\/code>\u65b9\u6cd5\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u6309\u7167\u7a7a\u683c\u5206\u9694\u6210\u591a\u4e2a\u5b50\u5b57\u7b26\u4e32\uff0c<code>map<\/code>\u51fd\u6570\u5c06\u6bcf\u4e2a\u5b50\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u6700\u540e\u5c06\u8fd9\u4e9b\u6574\u6570\u5b58\u50a8\u5728<code>numbers<\/code>\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u51fd\u6570\u5c01\u88c5\u8f93\u5165\u903b\u8f91<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u91cd\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8f93\u5165\u591a\u4e2a\u6570\u7684\u903b\u8f91\u5c01\u88c5\u5230\u4e00\u4e2a\u51fd\u6570\u4e2d\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def input_numbers(n):<\/p>\n<p>    numbers = []<\/p>\n<p>    for _ in range(n):<\/p>\n<p>        num = int(input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570: &quot;))<\/p>\n<p>        numbers.append(num)<\/p>\n<p>    return numbers<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n<p>numbers = input_numbers(5)<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6574\u6570\u5217\u8868:&quot;, numbers)<\/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>input_numbers<\/code>\u7684\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570<code>n<\/code>\uff0c\u8868\u793a\u9700\u8981\u8f93\u5165\u7684\u6574\u6570\u4e2a\u6570\u3002\u51fd\u6570\u5185\u90e8\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u63d0\u793a\u7528\u6237\u8f93\u5165\u6574\u6570\uff0c\u5e76\u5c06\u8f93\u5165\u7684\u6574\u6570\u5b58\u50a8\u5728<code>numbers<\/code>\u5217\u8868\u4e2d\uff0c\u6700\u540e\u8fd4\u56de\u5217\u8868\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u91cd\u590d\u4f7f\u7528\u8f93\u5165\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u5f02\u5e38\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7528\u6237\u53ef\u80fd\u4f1a\u8f93\u5165\u975e\u6574\u6570\u503c\u6216\u5176\u4ed6\u65e0\u6548\u6570\u636e\u3002\u4e3a\u4e86\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff0c\u6211\u4eec\u9700\u8981\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u8f93\u5165\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def input_numbers(n):<\/p>\n<p>    numbers = []<\/p>\n<p>    for _ in range(n):<\/p>\n<p>        while True:<\/p>\n<p>            try:<\/p>\n<p>                num = int(input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570: &quot;))<\/p>\n<p>                numbers.append(num)<\/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 numbers<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n<p>numbers = input_numbers(5)<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6574\u6570\u5217\u8868:&quot;, numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728<code>while<\/code>\u5faa\u73af\u4e2d\u4f7f\u7528\u4e86<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u7528\u6237\u8f93\u5165\u7684\u5f02\u5e38\u3002\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u4e0d\u662f\u6574\u6570\uff0c\u7a0b\u5e8f\u4f1a\u63d0\u793a\u8f93\u5165\u65e0\u6548\uff0c\u5e76\u8981\u6c42\u91cd\u65b0\u8f93\u5165\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6700\u7ec8\u5b58\u50a8\u5728<code>numbers<\/code>\u5217\u8868\u4e2d\u7684\u90fd\u662f\u6709\u6548\u7684\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4ece\u6587\u4ef6\u8bfb\u53d6\u591a\u4e2a\u6570<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u9700\u8981\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u591a\u4e2a\u6570\uff0c\u5e76\u5c06\u5b83\u4eec\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u6587\u4ef6<code>numbers.txt<\/code>\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u4e00\u884c\u6216\u591a\u884c\u6574\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code>1 2 3 4 5<\/p>\n<p>6 7 8 9 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684\u6574\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def read_numbers_from_file(filename):<\/p>\n<p>    numbers = []<\/p>\n<p>    with open(filename, &#39;r&#39;) as file:<\/p>\n<p>        for line in file:<\/p>\n<p>            numbers.extend(map(int, line.split()))<\/p>\n<p>    return numbers<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n<p>numbers = read_numbers_from_file(&#39;numbers.txt&#39;)<\/p>\n<p>print(&quot;\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u7684\u6574\u6570\u5217\u8868:&quot;, numbers)<\/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>read_numbers_from_file<\/code>\u7684\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570<code>filename<\/code>\uff0c\u8868\u793a\u6587\u4ef6\u540d\u3002\u51fd\u6570\u5185\u90e8\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u5bf9\u4e8e\u6bcf\u4e00\u884c\uff0c\u6211\u4eec\u4f7f\u7528<code>split<\/code>\u65b9\u6cd5\u5c06\u884c\u4e2d\u7684\u5b57\u7b26\u4e32\u5206\u9694\u6210\u591a\u4e2a\u5b50\u5b57\u7b26\u4e32\uff0c<code>map<\/code>\u51fd\u6570\u5c06\u6bcf\u4e2a\u5b50\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5e76\u5c06\u8fd9\u4e9b\u6574\u6570\u6dfb\u52a0\u5230<code>numbers<\/code>\u5217\u8868\u4e2d\u3002\u6700\u540e\u8fd4\u56de\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u751f\u6210\u5668\u8f93\u5165\u591a\u4e2a\u6570<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b83\u6765\u5b9e\u73b0\u591a\u6b21\u8f93\u5165\u3002\u751f\u6210\u5668\u5728\u6bcf\u6b21\u8fed\u4ee3\u65f6\u751f\u6210\u4e00\u4e2a\u503c\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u751f\u6210\u6240\u6709\u503c\u3002\u8fd9\u6837\u53ef\u4ee5\u51cf\u5c11\u5185\u5b58\u6d88\u8017\uff0c\u9002\u5408\u5904\u7406\u5927\u91cf\u6570\u636e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def input_numbers_generator(n):<\/p>\n<p>    for _ in range(n):<\/p>\n<p>        yield int(input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570: &quot;))<\/p>\n<h2><strong>\u8c03\u7528\u751f\u6210\u5668<\/strong><\/h2>\n<p>numbers = list(input_numbers_generator(5))<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6574\u6570\u5217\u8868:&quot;, numbers)<\/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>input_numbers_generator<\/code>\u7684\u751f\u6210\u5668\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570<code>n<\/code>\uff0c\u8868\u793a\u9700\u8981\u8f93\u5165\u7684\u6574\u6570\u4e2a\u6570\u3002\u51fd\u6570\u5185\u90e8\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u63d0\u793a\u7528\u6237\u8f93\u5165\u6574\u6570\uff0c\u5e76\u901a\u8fc7<code>yield<\/code>\u5173\u952e\u5b57\u751f\u6210\u6bcf\u4e2a\u6574\u6570\u3002\u751f\u6210\u5668\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<code>list<\/code>\u51fd\u6570\u5c06\u751f\u6210\u5668\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4ece\u547d\u4ee4\u884c\u53c2\u6570\u83b7\u53d6\u591a\u4e2a\u6570<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u5e0c\u671b\u4ece\u547d\u4ee4\u884c\u53c2\u6570\u4e2d\u83b7\u53d6\u591a\u4e2a\u6570\uff0c\u5e76\u5c06\u5b83\u4eec\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>def get_numbers_from_args():<\/p>\n<p>    return list(map(int, sys.argv[1:]))<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n<p>if __name__ == &quot;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&quot;:<\/p>\n<p>    numbers = get_numbers_from_args()<\/p>\n<p>    print(&quot;\u4ece\u547d\u4ee4\u884c\u53c2\u6570\u83b7\u53d6\u7684\u6574\u6570\u5217\u8868:&quot;, numbers)<\/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_numbers_from_args<\/code>\u7684\u51fd\u6570\uff0c\u5b83\u4f7f\u7528<code>sys.argv<\/code>\u83b7\u53d6\u547d\u4ee4\u884c\u53c2\u6570\u3002<code>sys.argv<\/code>\u662f\u4e00\u4e2a\u5305\u542b\u547d\u4ee4\u884c\u53c2\u6570\u7684\u5217\u8868\uff0c\u5176\u4e2d\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u811a\u672c\u540d\uff0c\u540e\u7eed\u5143\u7d20\u662f\u4f20\u9012\u7ed9\u811a\u672c\u7684\u53c2\u6570\u3002\u6211\u4eec\u4f7f\u7528<code>map<\/code>\u51fd\u6570\u5c06\u53c2\u6570\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5e76\u8fd4\u56de\u6574\u6570\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u7efc\u5408\u5b9e\u4f8b\uff1a\u8f93\u5165\u591a\u4e2a\u6570\u5e76\u8fdb\u884c\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8f93\u5165\u591a\u4e2a\u6570\u540e\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u8fd9\u4e9b\u6570\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u5e73\u5747\u503c\u3001\u6700\u5927\u503c\u3001\u6700\u5c0f\u503c\u7b49\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7efc\u5408\u5b9e\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def input_numbers(n):<\/p>\n<p>    numbers = []<\/p>\n<p>    for _ in range(n):<\/p>\n<p>        while True:<\/p>\n<p>            try:<\/p>\n<p>                num = int(input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570: &quot;))<\/p>\n<p>                numbers.append(num)<\/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 numbers<\/p>\n<p>def calculate_statistics(numbers):<\/p>\n<p>    if not numbers:<\/p>\n<p>        return None<\/p>\n<p>    total = sum(numbers)<\/p>\n<p>    count = len(numbers)<\/p>\n<p>    average = total \/ count<\/p>\n<p>    maximum = max(numbers)<\/p>\n<p>    minimum = min(numbers)<\/p>\n<p>    return {<\/p>\n<p>        &#39;total&#39;: total,<\/p>\n<p>        &#39;count&#39;: count,<\/p>\n<p>        &#39;average&#39;: average,<\/p>\n<p>        &#39;max&#39;: maximum,<\/p>\n<p>        &#39;min&#39;: minimum<\/p>\n<p>    }<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n<p>numbers = input_numbers(5)<\/p>\n<p>stats = calculate_statistics(numbers)<\/p>\n<p>if stats:<\/p>\n<p>    print(&quot;\u603b\u548c:&quot;, stats[&#39;total&#39;])<\/p>\n<p>    print(&quot;\u6570\u91cf:&quot;, stats[&#39;count&#39;])<\/p>\n<p>    print(&quot;\u5e73\u5747\u503c:&quot;, stats[&#39;average&#39;])<\/p>\n<p>    print(&quot;\u6700\u5927\u503c:&quot;, stats[&#39;max&#39;])<\/p>\n<p>    print(&quot;\u6700\u5c0f\u503c:&quot;, stats[&#39;min&#39;])<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u6ca1\u6709\u8f93\u5165\u4efb\u4f55\u6709\u6548\u7684\u6574\u6570\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7efc\u5408\u5b9e\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86<code>input_numbers<\/code>\u51fd\u6570\u6765\u8f93\u5165\u591a\u4e2a\u6574\u6570\uff0c\u5e76\u5904\u7406\u5f02\u5e38\u8f93\u5165\u3002\u7136\u540e\u5b9a\u4e49\u4e86<code>calculate_statistics<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u8f93\u5165\u6574\u6570\u7684\u603b\u548c\u3001\u6570\u91cf\u3001\u5e73\u5747\u503c\u3001\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u3002\u6700\u540e\u8c03\u7528\u8fd9\u4e24\u4e2a\u51fd\u6570\uff0c\u5e76\u8f93\u51fa\u7edf\u8ba1\u7ed3\u679c\u3002\u5982\u679c\u7528\u6237\u6ca1\u6709\u8f93\u5165\u4efb\u4f55\u6709\u6548\u7684\u6574\u6570\uff0c\u7a0b\u5e8f\u4f1a\u63d0\u793a\u6ca1\u6709\u8f93\u5165\u4efb\u4f55\u6709\u6548\u7684\u6574\u6570\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u5faa\u73af\u8f93\u5165\u591a\u4e2a\u6570\u5b57\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u5229\u7528<code>for<\/code>\u5faa\u73af\u6216<code>while<\/code>\u5faa\u73af\u6765\u591a\u6b21\u8f93\u5165\u6570\u5b57\u3002\u901a\u5e38\uff0c\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u6765\u8bfb\u53d6\u7528\u6237\u7684\u8f93\u5165\uff0c\u7136\u540e\u5c06\u8f93\u5165\u7684\u6570\u5b57\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u5904\u7406\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u6307\u5b9a\u8f93\u5165\u7684\u6b21\u6570\uff0c\u6216\u8005\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u76f4\u5230\u7528\u6237\u8f93\u5165\u7279\u5b9a\u7684\u7ed3\u675f\u6807\u5fd7\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u8f93\u5165\u7684\u591a\u4e2a\u6570\u5b57\u4ee5\u8fdb\u884c\u8ba1\u7b97\uff1f<\/strong><br \/>\u5728\u83b7\u53d6\u591a\u4e2a\u6570\u5b57\u540e\uff0c\u53ef\u4ee5\u5bf9\u5b83\u4eec\u8fdb\u884c\u5404\u79cd\u8ba1\u7b97\uff0c\u5982\u6c42\u548c\u3001\u5e73\u5747\u503c\u6216\u6700\u5927\u503c\u7b49\u3002\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u5b58\u50a8\u6570\u5b57\u7684\u5217\u8868\uff0c\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u5982<code>sum()<\/code>\u3001<code>max()<\/code>\u7b49\u6765\u5b9e\u73b0\u8fd9\u4e9b\u8ba1\u7b97\u3002\u786e\u4fdd\u5728\u8f93\u5165\u65f6\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\u7c7b\u578b\uff08\u5982<code>int<\/code>\u6216<code>float<\/code>\uff09\uff0c\u4ee5\u907f\u514d\u8ba1\u7b97\u9519\u8bef\u3002<\/p>\n<p><strong>\u6709\u4ec0\u4e48\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u6570\u5b57\u6709\u6548\u6027\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u6570\u5b57\u6709\u6548\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u6355\u83b7\u53ef\u80fd\u7684\u5f02\u5e38\u3002\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u4e0d\u662f\u6570\u5b57\uff0c\u53ef\u4ee5\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002\u53ef\u4ee5\u7ed3\u5408\u5faa\u73af\u7ed3\u6784\u6765\u53cd\u590d\u8bf7\u6c42\u8f93\u5165\uff0c\u76f4\u5230\u83b7\u5f97\u6709\u6548\u7684\u6570\u5b57\uff0c\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5229\u7528\u5faa\u73af\u540c\u65f6\u8f93\u5165\u591a\u4e2a\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528for\u5faa\u73af\u3001while\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u65b9\u6cd5\u6765\u5b9e\u73b0\u3001\u8fd9\u4e9b\u65b9\u6cd5\u53ef [&hellip;]","protected":false},"author":3,"featured_media":1049045,"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\/1049041"}],"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=1049041"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1049041\/revisions"}],"predecessor-version":[{"id":1049049,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1049041\/revisions\/1049049"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1049045"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1049041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1049041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1049041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}