{"id":1111206,"date":"2025-01-08T17:26:53","date_gmt":"2025-01-08T09:26:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1111206.html"},"modified":"2025-01-08T17:26:56","modified_gmt":"2025-01-08T09:26:56","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a9%e7%94%a8%e6%88%b7%e8%be%93%e5%85%a5%e4%b8%80%e4%b8%aa%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1111206.html","title":{"rendered":"python\u5982\u4f55\u8ba9\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u5217\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073751\/ad9ff637-9e1c-4f1c-a25e-85ea350acdda.webp\" alt=\"python\u5982\u4f55\u8ba9\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u5217\u8868\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u8ba9\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u5217\u8868\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u901a\u8fc7input\u51fd\u6570\u63a5\u6536\u7528\u6237\u8f93\u5165\u3001\u4f7f\u7528split\u65b9\u6cd5\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u3001\u4f7f\u7528\u5faa\u73af\u63a5\u6536\u591a\u6b21\u8f93\u5165\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7input\u51fd\u6570\u548csplit\u65b9\u6cd5\u7ed3\u5408\u6765\u5b9e\u73b0\u3002\u63a5\u4e0b\u6765\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528input()\u51fd\u6570\u6765\u63a5\u6536\u7528\u6237\u8f93\u5165\uff0c\u5e76\u4f7f\u7528split()\u65b9\u6cd5\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u4f8b\u5982\uff0c\u7528\u6237\u53ef\u4ee5\u8f93\u5165\u4e00\u884c\u4ee5\u7a7a\u683c\u5206\u9694\u7684\u6570\u5b57\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u5316\u4e3a\u4e00\u4e2a\u6574\u6570\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528input\u51fd\u6570\u548csplit\u65b9\u6cd5\u63a5\u6536\u5217\u8868\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7input\u51fd\u6570\u63a5\u6536\u7528\u6237\u8f93\u5165\uff0c\u7136\u540e\u4f7f\u7528split\u65b9\u6cd5\u5c06\u8f93\u5165\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5217\u8868\u3002\u8fd9\u662f\u6700\u5e38\u89c1\u4e14\u7b80\u4fbf\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u63a5\u6536\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32<\/p>\n<p>user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u7ec4\u4ee5\u7a7a\u683c\u5206\u9694\u7684\u6570\u5b57\uff1a&quot;)<\/p>\n<h2><strong>\u4f7f\u7528split\u65b9\u6cd5\u5c06\u8f93\u5165\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5217\u8868<\/strong><\/h2>\n<p>input_list = user_input.split()<\/p>\n<h2><strong>\u5c06\u5b57\u7b26\u4e32\u5217\u8868\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868<\/strong><\/h2>\n<p>input_list = [int(item) for item in input_list]<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u5217\u8868\u4e3a\uff1a&quot;, input_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7528\u6237\u8f93\u5165\u4e00\u884c\u6570\u5b57\uff0c\u7a0b\u5e8f\u4f1a\u5c06\u5176\u8f6c\u5316\u4e3a\u4e00\u4e2a\u6574\u6570\u5217\u8868\u3002<strong>split\u65b9\u6cd5\u53ef\u4ee5\u6309\u7167\u7a7a\u683c\u3001\u9017\u53f7\u7b49\u5206\u9694\u7b26\u6765\u5206\u5272\u5b57\u7b26\u4e32\uff0c\u7075\u6d3b\u6027\u5f88\u9ad8\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5faa\u73af\u63a5\u6536\u591a\u6b21\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7528\u6237\u53ef\u80fd\u9700\u8981\u591a\u6b21\u8f93\u5165\u6765\u6784\u6210\u5217\u8868\u3002\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">input_list = []<\/p>\n<p>while True:<\/p>\n<p>    user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff08\u8f93\u5165&#39;\u7ed3\u675f&#39;\u505c\u6b62\u8f93\u5165\uff09\uff1a&quot;)<\/p>\n<p>    if user_input.lower() == &#39;\u7ed3\u675f&#39;:<\/p>\n<p>        break<\/p>\n<p>    else:<\/p>\n<p>        input_list.append(int(user_input))<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u5217\u8868\u4e3a\uff1a&quot;, input_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7528\u6237\u53ef\u4ee5\u4e0d\u65ad\u8f93\u5165\u6570\u5b57\uff0c\u76f4\u5230\u8f93\u5165\u201c\u7ed3\u675f\u201d\u4e3a\u6b62\uff0c\u7a0b\u5e8f\u4f1a\u5c06\u6240\u6709\u8f93\u5165\u7684\u6570\u5b57\u5b58\u5165\u5217\u8868\u4e2d\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u7528\u6237\u9700\u8981\u9010\u6b65\u8f93\u5165\u591a\u4e2a\u5143\u7d20\u7684\u573a\u666f\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u7279\u6b8a\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7528\u6237\u7684\u8f93\u5165\u53ef\u80fd\u4f1a\u5305\u542b\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\uff0c\u5982\u8f93\u5165\u975e\u6570\u5b57\u5b57\u7b26\u3001\u7a7a\u8f93\u5165\u7b49\u3002\u9700\u8981\u6dfb\u52a0\u76f8\u5e94\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">input_list = []<\/p>\n<p>while True:<\/p>\n<p>    user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff08\u8f93\u5165&#39;\u7ed3\u675f&#39;\u505c\u6b62\u8f93\u5165\uff09\uff1a&quot;)<\/p>\n<p>    if user_input.lower() == &#39;\u7ed3\u675f&#39;:<\/p>\n<p>        break<\/p>\n<p>    elif user_input.strip() == &#39;&#39;:<\/p>\n<p>        print(&quot;\u8f93\u5165\u4e3a\u7a7a\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\u3002&quot;)<\/p>\n<p>    else:<\/p>\n<p>        try:<\/p>\n<p>            input_list.append(int(user_input))<\/p>\n<p>        except ValueError:<\/p>\n<p>            print(&quot;\u8f93\u5165\u7684\u4e0d\u662f\u6709\u6548\u7684\u6570\u5b57\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\u3002&quot;)<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u5217\u8868\u4e3a\uff1a&quot;, input_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u68c0\u67e5\u7528\u6237\u8f93\u5165\u662f\u5426\u4e3a\u7a7a\uff0c\u662f\u5426\u4e3a\u6709\u6548\u6570\u5b57\uff0c\u5e76\u5728\u51fa\u73b0\u9519\u8bef\u65f6\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u6709\u6548\u63d0\u9ad8\u7528\u6237\u8f93\u5165\u7684\u51c6\u786e\u6027\u548c\u7a0b\u5e8f\u7684\u9c81\u68d2\u6027\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u56db\u3001\u63a5\u6536\u591a\u79cd\u7c7b\u578b\u7684\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u7528\u6237\u8f93\u5165\u7684\u5217\u8868\u53ef\u80fd\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u6574\u6570\u7b49\u3002\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u7ec4\u4ee5\u7a7a\u683c\u5206\u9694\u7684\u5143\u7d20\uff08\u53ef\u4ee5\u5305\u542b\u5b57\u7b26\u4e32\u548c\u6570\u5b57\uff09\uff1a&quot;)<\/p>\n<h2><strong>\u4f7f\u7528split\u65b9\u6cd5\u5c06\u8f93\u5165\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5217\u8868<\/strong><\/h2>\n<p>input_list = user_input.split()<\/p>\n<h2><strong>\u5c1d\u8bd5\u5c06\u6bcf\u4e2a\u5143\u7d20\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5982\u679c\u8f6c\u6362\u5931\u8d25\u5219\u4fdd\u7559\u539f\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>def convert_to_int(item):<\/p>\n<p>    try:<\/p>\n<p>        return int(item)<\/p>\n<p>    except ValueError:<\/p>\n<p>        return item<\/p>\n<p>input_list = [convert_to_int(item) for item in input_list]<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u5217\u8868\u4e3a\uff1a&quot;, input_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u5c1d\u8bd5\u5c06\u6bcf\u4e2a\u5143\u7d20\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5982\u679c\u8f6c\u6362\u5931\u8d25\u5219\u4fdd\u7559\u539f\u5b57\u7b26\u4e32\u3002<strong>\u8fd9\u6837\u53ef\u4ee5\u7075\u6d3b\u5904\u7406\u5305\u542b\u591a\u79cd\u7c7b\u578b\u7684\u5217\u8868\u8f93\u5165\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u7528\u6237\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728Python\u4e2d\u8f93\u5165\u4e00\u4e2a\u5217\u8868\u3002<strong>\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7input\u51fd\u6570\u548csplit\u65b9\u6cd5\u7ed3\u5408\u6765\u5b9e\u73b0\uff0c\u53ef\u4ee5\u5904\u7406\u591a\u79cd\u7c7b\u578b\u7684\u8f93\u5165\uff0c\u5e76\u4e14\u80fd\u591f\u6dfb\u52a0\u9519\u8bef\u5904\u7406\u673a\u5236\uff0c\u4fdd\u8bc1\u8f93\u5165\u7684\u51c6\u786e\u6027\u548c\u7a0b\u5e8f\u7684\u9c81\u68d2\u6027\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u5217\u8868\u3002\u65e0\u8bba\u662f\u4e00\u6b21\u6027\u8f93\u5165\u591a\u4e2a\u5143\u7d20\uff0c\u8fd8\u662f\u9010\u4e2a\u8f93\u5165\u5143\u7d20\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u89e3\u51b3\u65b9\u6848\u3002\u5e0c\u671b\u4ee5\u4e0a\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u4f60\u5728Python\u7f16\u7a0b\u4e2d\u53d6\u5f97\u66f4\u5927\u7684\u8fdb\u6b65\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u63d0\u793a\u7528\u6237\u8f93\u5165\u591a\u4e2a\u503c\u5e76\u5c06\u5176\u5b58\u50a8\u4e3a\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u63d0\u793a\u7528\u6237\u8f93\u5165\u591a\u4e2a\u503c\u3002\u4e3a\u4e86\u5c06\u8fd9\u4e9b\u503c\u5b58\u50a8\u4e3a\u5217\u8868\uff0c\u60a8\u53ef\u4ee5\u8981\u6c42\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u4ee5\u9017\u53f7\u5206\u9694\u7684\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">user_input = input(&quot;\u8bf7\u8f93\u5165\u591a\u4e2a\u503c\uff08\u7528\u9017\u53f7\u5206\u9694\uff09\uff1a&quot;)\nuser_list = user_input.split(&#39;,&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u5c31\u4f1a\u88ab\u5206\u5272\u6210\u4e00\u4e2a\u5217\u8868\u3002<\/p>\n<p><strong>\u7528\u6237\u8f93\u5165\u7684\u503c\u53ef\u4ee5\u662f\u54ea\u4e9b\u7c7b\u578b\uff1f<\/strong><br \/>\u7528\u6237\u8f93\u5165\u7684\u503c\u53ef\u4ee5\u662f\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u751a\u81f3\u662f\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u3002\u82e5\u5e0c\u671b\u5c06\u8f93\u5165\u7684\u6570\u5b57\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u5728\u5206\u5272\u540e\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">user_input = input(&quot;\u8bf7\u8f93\u5165\u591a\u4e2a\u6570\u5b57\uff08\u7528\u9017\u53f7\u5206\u9694\uff09\uff1a&quot;)\nuser_list = [float(x) for x in user_input.split(&#39;,&#39;)]\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u5c06\u88ab\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u5217\u8868\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u7528\u6237\u8f93\u5165\u4e2d\u7684\u9519\u8bef\u6216\u7a7a\u503c\uff1f<\/strong><br \/>\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u7ed3\u6784\u6765\u6355\u83b7\u53ef\u80fd\u7684\u9519\u8bef\uff0c\u5e76\u5bf9\u8f93\u5165\u8fdb\u884c\u9a8c\u8bc1\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u68c0\u67e5\u7528\u6237\u662f\u5426\u8f93\u5165\u4e86\u7a7a\u503c\u6216\u975e\u6570\u5b57\u5b57\u7b26\uff1a<\/p>\n<pre><code class=\"language-python\">user_input = input(&quot;\u8bf7\u8f93\u5165\u591a\u4e2a\u6570\u5b57\uff08\u7528\u9017\u53f7\u5206\u9694\uff09\uff1a&quot;)\ntry:\n    user_list = [float(x) for x in user_input.split(&#39;,&#39;) if x.strip()]\nexcept ValueError:\n    print(&quot;\u8bf7\u786e\u4fdd\u60a8\u8f93\u5165\u7684\u662f\u6570\u5b57\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u5c31\u80fd\u6709\u6548\u5730\u5904\u7406\u7528\u6237\u7684\u9519\u8bef\u8f93\u5165\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u8ba9\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u5217\u8868\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u901a\u8fc7input\u51fd\u6570\u63a5\u6536\u7528\u6237\u8f93\u5165\u3001\u4f7f\u7528spli [&hellip;]","protected":false},"author":3,"featured_media":1111212,"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\/1111206"}],"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=1111206"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1111206\/revisions"}],"predecessor-version":[{"id":1111214,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1111206\/revisions\/1111214"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1111212"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1111206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1111206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1111206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}