{"id":1179329,"date":"2025-01-15T18:22:09","date_gmt":"2025-01-15T10:22:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1179329.html"},"modified":"2025-01-15T18:22:09","modified_gmt":"2025-01-15T10:22:09","slug":"python%e5%a6%82%e4%bd%95%e7%94%a8input%e6%b7%bb%e5%8a%a0%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1179329.html","title":{"rendered":"python\u5982\u4f55\u7528input\u6dfb\u52a0\u5217\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25113808\/f2f17d80-e6ea-4254-b54a-2e028621bbb6.webp\" alt=\"python\u5982\u4f55\u7528input\u6dfb\u52a0\u5217\u8868\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528input\u51fd\u6570\u4e0e\u5217\u8868\u64cd\u4f5c\u6765\u5b9e\u73b0\u52a8\u6001\u6dfb\u52a0\u5143\u7d20<\/strong>\u3001<strong>\u5faa\u73af\u83b7\u53d6\u7528\u6237\u8f93\u5165\u5e76\u6dfb\u52a0\u5230\u5217\u8868\u4e2d<\/strong>\u3001<strong>\u4f7f\u7528append\u65b9\u6cd5\u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u4e2d<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u70b9\uff0c\u5373<strong>\u5faa\u73af\u83b7\u53d6\u7528\u6237\u8f93\u5165\u5e76\u6dfb\u52a0\u5230\u5217\u8868\u4e2d<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u4e0d\u65ad\u83b7\u53d6\u7528\u6237\u8f93\u5165\uff0c\u5c06\u5176\u6dfb\u52a0\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u76f4\u5230\u7528\u6237\u9009\u62e9\u505c\u6b62\u8f93\u5165\u4e3a\u6b62\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2awhile\u5faa\u73af\u6765\u5b9e\u73b0\uff0c\u5faa\u73af\u5185\u90e8\u4f7f\u7528input\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\uff0c\u7136\u540e\u5c06\u8f93\u5165\u7684\u5185\u5bb9\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u8f93\u5165\u7279\u5b9a\u7684\u5b57\u7b26\uff08\u4f8b\u5982\u201cq\u201d\u6216\u201cexit\u201d\uff09\u6765\u7ec8\u6b62\u8f93\u5165\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u7a7a\u5217\u8868<\/p>\n<p>my_list = []<\/p>\n<h2><strong>\u5faa\u73af\u83b7\u53d6\u7528\u6237\u8f93\u5165<\/strong><\/h2>\n<p>while True:<\/p>\n<p>    user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u5143\u7d20\uff08\u8f93\u5165&#39;q&#39;\u7ed3\u675f\uff09\uff1a&quot;)<\/p>\n<p>    if user_input.lower() == &#39;q&#39;:<\/p>\n<p>        break<\/p>\n<p>    my_list.append(user_input)<\/p>\n<p>print(&quot;\u6700\u7ec8\u7684\u5217\u8868\uff1a&quot;, my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u5217\u8868<code>my_list<\/code>\uff0c\u7136\u540e\u8fdb\u5165\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u3002\u6bcf\u6b21\u5faa\u73af\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u5143\u7d20\uff0c\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u662f\u201cq\u201d\uff0c\u5219\u9000\u51fa\u5faa\u73af\uff1b\u5426\u5219\uff0c\u5c06\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002\u6700\u7ec8\uff0c\u8f93\u51fa\u5b8c\u6574\u7684\u5217\u8868\u5185\u5bb9\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u521d\u59cb\u5316\u7a7a\u5217\u8868\u4e0e\u8f93\u5165\u63d0\u793a<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u52a8\u6001\u6dfb\u52a0\u5143\u7d20\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u5217\u8868\uff0c\u5e76\u8bbe\u8ba1\u4e00\u4e2a\u8f93\u5165\u63d0\u793a\uff0c\u4ee5\u4fbf\u7528\u6237\u660e\u767d\u9700\u8981\u8f93\u5165\u54ea\u4e9b\u5185\u5bb9\u3002Python\u7684\u5217\u8868\u521d\u59cb\u5316\u975e\u5e38\u7b80\u5355\uff0c\u6211\u4eec\u53ea\u9700\u4f7f\u7528\u4e00\u5bf9\u65b9\u62ec\u53f7\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = []<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e3a\u4e86\u8ba9\u7528\u6237\u660e\u767d\u9700\u8981\u8f93\u5165\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u63d0\u4f9b\u4e00\u4e2a\u63d0\u793a\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u5143\u7d20\uff1a&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u884c\u4ee3\u7801\u4f1a\u5728\u8fd0\u884c\u65f6\u6682\u505c\u7a0b\u5e8f\uff0c\u5e76\u7b49\u5f85\u7528\u6237\u8f93\u5165\u5185\u5bb9\u3002\u8f93\u5165\u7684\u5185\u5bb9\u4f1a\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u5b58\u50a8\u5728\u53d8\u91cf<code>user_input<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u83b7\u53d6\u7528\u6237\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u5b9e\u73b0\u52a8\u6001\u6dfb\u52a0\u5143\u7d20\uff0c\u6211\u4eec\u9700\u8981\u4e0d\u65ad\u83b7\u53d6\u7528\u6237\u8f93\u5165\uff0c\u76f4\u5230\u7528\u6237\u9009\u62e9\u505c\u6b62\u8f93\u5165\u3002\u53ef\u4ee5\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u5143\u7d20\uff08\u8f93\u5165&#39;q&#39;\u7ed3\u675f\uff09\uff1a&quot;)<\/p>\n<p>    if user_input.lower() == &#39;q&#39;:<\/p>\n<p>        break<\/p>\n<p>    my_list.append(user_input)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528<code>while True<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u3002\u6bcf\u6b21\u5faa\u73af\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u5143\u7d20\u3002\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u662f\u201cq\u201d\uff0c\u5219<code>if<\/code>\u8bed\u53e5\u5224\u65ad\u4e3a\u771f\uff0c\u6267\u884c<code>break<\/code>\u8bed\u53e5\u9000\u51fa\u5faa\u73af\uff1b\u5426\u5219\uff0c\u5c06\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u6dfb\u52a0\u5230\u5217\u8868<code>my_list<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6dfb\u52a0\u5143\u7d20\u5230\u5217\u8868\u4e2d<\/h3>\n<\/p>\n<p><p>\u5728\u83b7\u53d6\u7528\u6237\u8f93\u5165\u540e\uff0c\u6211\u4eec\u9700\u8981\u5c06\u8f93\u5165\u7684\u5185\u5bb9\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002Python\u4e2d\u53ef\u4ee5\u4f7f\u7528<code>append()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list.append(user_input)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>append()<\/code>\u65b9\u6cd5\u4f1a\u5c06\u53c2\u6570\u4e2d\u7684\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u7684\u672b\u5c3e\uff0c\u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86\u52a8\u6001\u6dfb\u52a0\u5143\u7d20\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8f93\u51fa\u6700\u7ec8\u7684\u5217\u8868\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u5728\u7528\u6237\u5b8c\u6210\u8f93\u5165\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u8f93\u51fa\u6700\u7ec8\u7684\u5217\u8868\u5185\u5bb9\uff0c\u4ee5\u4fbf\u7528\u6237\u67e5\u770b\u81ea\u5df1\u8f93\u5165\u7684\u6240\u6709\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;\u6700\u7ec8\u7684\u5217\u8868\uff1a&quot;, my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u884c\u4ee3\u7801\u4f1a\u8f93\u51fa\u5b8c\u6574\u7684\u5217\u8868\u5185\u5bb9\uff0c\u5e2e\u52a9\u7528\u6237\u786e\u8ba4\u8f93\u5165\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e94\u3001\u6269\u5c55\u529f\u80fd\uff1a\u9650\u5236\u8f93\u5165\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u9650\u5236\u7528\u6237\u8f93\u5165\u7684\u7c7b\u578b\uff0c\u4f8b\u5982\u53ea\u5141\u8bb8\u8f93\u5165\u6574\u6570\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5728\u8f93\u5165\u540e\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u548c\u5f02\u5e38\u5904\u7406\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = []<\/p>\n<p>while True:<\/p>\n<p>    user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570\uff08\u8f93\u5165&#39;q&#39;\u7ed3\u675f\uff09\uff1a&quot;)<\/p>\n<p>    if user_input.lower() == &#39;q&#39;:<\/p>\n<p>        break<\/p>\n<p>    try:<\/p>\n<p>        user_input = int(user_input)<\/p>\n<p>        my_list.append(user_input)<\/p>\n<p>    except ValueError:<\/p>\n<p>        print(&quot;\u8bf7\u8f93\u5165\u6709\u6548\u7684\u6574\u6570\u3002&quot;)<\/p>\n<p>print(&quot;\u6700\u7ec8\u7684\u5217\u8868\uff1a&quot;, my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5728\u5c06\u7528\u6237\u8f93\u5165\u6dfb\u52a0\u5230\u5217\u8868\u4e4b\u524d\uff0c\u5c1d\u8bd5\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u5982\u679c\u8f6c\u6362\u5931\u8d25\uff0c\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\uff0c\u6211\u4eec\u53ef\u4ee5\u6355\u83b7\u8fd9\u4e2a\u5f02\u5e38\u5e76\u63d0\u793a\u7528\u6237\u8f93\u5165\u6709\u6548\u7684\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6269\u5c55\u529f\u80fd\uff1a\u5904\u7406\u91cd\u590d\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u53ef\u80fd\u4e0d\u5e0c\u671b\u5217\u8868\u4e2d\u5305\u542b\u91cd\u590d\u7684\u5143\u7d20\uff0c\u8fd9\u65f6\u53ef\u4ee5\u5728\u6dfb\u52a0\u65b0\u5143\u7d20\u524d\u68c0\u67e5\u5217\u8868\u4e2d\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u8be5\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = []<\/p>\n<p>while True:<\/p>\n<p>    user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u5143\u7d20\uff08\u8f93\u5165&#39;q&#39;\u7ed3\u675f\uff09\uff1a&quot;)<\/p>\n<p>    if user_input.lower() == &#39;q&#39;:<\/p>\n<p>        break<\/p>\n<p>    if user_input in my_list:<\/p>\n<p>        print(&quot;\u8be5\u5143\u7d20\u5df2\u5b58\u5728\uff0c\u8bf7\u8f93\u5165\u5176\u4ed6\u5143\u7d20\u3002&quot;)<\/p>\n<p>    else:<\/p>\n<p>        my_list.append(user_input)<\/p>\n<p>print(&quot;\u6700\u7ec8\u7684\u5217\u8868\uff1a&quot;, my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5728\u6dfb\u52a0\u65b0\u5143\u7d20\u524d\uff0c\u4f7f\u7528<code>if user_input in my_list<\/code>\u68c0\u67e5\u5217\u8868\u4e2d\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u8be5\u5143\u7d20\u3002\u5982\u679c\u5b58\u5728\uff0c\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\uff1b\u5426\u5219\uff0c\u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u6269\u5c55\u529f\u80fd\uff1a\u6839\u636e\u7528\u6237\u8f93\u5165\u51b3\u5b9a\u662f\u5426\u7ee7\u7eed<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u6839\u636e\u7528\u6237\u8f93\u5165\u7684\u5177\u4f53\u5185\u5bb9\u6765\u51b3\u5b9a\u662f\u5426\u7ee7\u7eed\u83b7\u53d6\u8f93\u5165\u3002\u4f8b\u5982\uff0c\u7528\u6237\u8f93\u5165\u7279\u5b9a\u7684\u5173\u952e\u5b57\uff08\u4f8b\u5982\u201cstop\u201d\uff09\u65f6\uff0c\u7ec8\u6b62\u8f93\u5165\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = []<\/p>\n<p>while True:<\/p>\n<p>    user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u5143\u7d20\uff08\u8f93\u5165&#39;stop&#39;\u7ed3\u675f\uff09\uff1a&quot;)<\/p>\n<p>    if user_input.lower() == &#39;stop&#39;:<\/p>\n<p>        break<\/p>\n<p>    my_list.append(user_input)<\/p>\n<p>print(&quot;\u6700\u7ec8\u7684\u5217\u8868\uff1a&quot;, my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u7ec8\u6b62\u8f93\u5165\u7684\u5173\u952e\u5b57\u8bbe\u4e3a\u201cstop\u201d\uff0c\u7528\u6237\u8f93\u5165\u8be5\u5173\u952e\u5b57\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u9000\u51fa\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u591a\u4e2a\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5728Python\u4e2d\u4f7f\u7528<code>input<\/code>\u51fd\u6570\u4e0e\u5217\u8868\u64cd\u4f5c\u6765\u52a8\u6001\u6dfb\u52a0\u5143\u7d20\u662f\u975e\u5e38\u7075\u6d3b\u548c\u5f3a\u5927\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u8bbe\u8ba1\u4e0d\u540c\u7684\u8f93\u5165\u63d0\u793a\u3001\u9650\u5236\u8f93\u5165\u7c7b\u578b\u3001\u5904\u7406\u91cd\u590d\u8f93\u5165\u3001\u4ee5\u53ca\u6839\u636e\u7528\u6237\u8f93\u5165\u51b3\u5b9a\u662f\u5426\u7ee7\u7eed\u7b49\u529f\u80fd\u3002\u8fd9\u6837\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u7a0b\u5e8f\u7684\u5b9e\u7528\u6027\u548c\u7528\u6237\u4f53\u9a8c\uff0c\u4e5f\u589e\u5f3a\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u7ed3\u5408\u5177\u4f53\u9700\u6c42\u548c\u7528\u6237\u4f53\u9a8c\uff0c\u53ef\u4ee5\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u4f7f\u5f97\u7a0b\u5e8f\u66f4\u52a0\u5065\u58ee\u548c\u5b9e\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7684input\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528input\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u9996\u5148\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868\uff0c\u7136\u540e\u901a\u8fc7\u5faa\u73af\u6216\u591a\u6b21\u8c03\u7528input\u51fd\u6570\uff0c\u8ba9\u7528\u6237\u8f93\u5165\u5143\u7d20\uff0c\u5e76\u5c06\u8fd9\u4e9b\u5143\u7d20\u9010\u4e2a\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">my_list = []\nwhile True:\n    item = input(&quot;\u8bf7\u8f93\u5165\u8981\u6dfb\u52a0\u5230\u5217\u8868\u7684\u5143\u7d20\uff08\u8f93\u5165&#39;\u7ed3\u675f&#39;\u6765\u505c\u6b62\uff09\uff1a&quot;)\n    if item.lower() == &#39;\u7ed3\u675f&#39;:\n        break\n    my_list.append(item)\nprint(&quot;\u60a8\u521b\u5efa\u7684\u5217\u8868\u662f\uff1a&quot;, my_list)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u5141\u8bb8\u7528\u6237\u52a8\u6001\u6dfb\u52a0\u4efb\u610f\u6570\u91cf\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u7b26\u5408\u7279\u5b9a\u6761\u4ef6\uff1f<\/strong><br \/>\u5728\u6536\u96c6\u7528\u6237\u8f93\u5165\u65f6\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u6761\u4ef6\u6765\u9a8c\u8bc1\u6570\u636e\u7684\u6709\u6548\u6027\u3002\u4f8b\u5982\uff0c\u8981\u6c42\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u5fc5\u987b\u662f\u6570\u5b57\u6216\u8005\u7279\u5b9a\u683c\u5f0f\u3002\u53ef\u4ee5\u4f7f\u7528try-except\u7ed3\u6784\u6765\u6355\u83b7\u8f93\u5165\u9519\u8bef\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">my_list = []\nwhile True:\n    item = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff08\u8f93\u5165&#39;\u7ed3\u675f&#39;\u6765\u505c\u6b62\uff09\uff1a&quot;)\n    if item.lower() == &#39;\u7ed3\u675f&#39;:\n        break\n    try:\n        number = float(item)  # \u5c1d\u8bd5\u5c06\u8f93\u5165\u8f6c\u6362\u4e3a\u6570\u5b57\n        my_list.append(number)\n    except ValueError:\n        print(&quot;\u65e0\u6548\u8f93\u5165\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\u3002&quot;)\nprint(&quot;\u60a8\u521b\u5efa\u7684\u6570\u5b57\u5217\u8868\u662f\uff1a&quot;, my_list)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u786e\u4fdd\u5217\u8868\u4e2d\u7684\u5185\u5bb9\u7b26\u5408\u9884\u671f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5217\u8868\u4e2d\u6dfb\u52a0\u591a\u4e2a\u5143\u7d20\u800c\u4e0d\u662f\u4e00\u4e2a\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u7528\u6237\u4e00\u6b21\u6027\u8f93\u5165\u591a\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u8981\u6c42\u7528\u6237\u4ee5\u7279\u5b9a\u5206\u9694\u7b26\uff08\u5982\u9017\u53f7\uff09\u8f93\u5165\uff0c\u7136\u540e\u4f7f\u7528split\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5217\u8868\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">my_list = input(&quot;\u8bf7\u8f93\u5165\u591a\u4e2a\u5143\u7d20\uff0c\u7528\u9017\u53f7\u5206\u9694\uff1a&quot;).split(&#39;,&#39;)\nmy_list = [item.strip() for item in my_list]  # \u53bb\u9664\u6bcf\u4e2a\u5143\u7d20\u524d\u540e\u7684\u7a7a\u683c\nprint(&quot;\u60a8\u521b\u5efa\u7684\u5217\u8868\u662f\uff1a&quot;, my_list)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u7528\u6237\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u6dfb\u52a0\u591a\u4e2a\u5143\u7d20\uff0c\u7b80\u5316\u8f93\u5165\u8fc7\u7a0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528input\u51fd\u6570\u4e0e\u5217\u8868\u64cd\u4f5c\u6765\u5b9e\u73b0\u52a8\u6001\u6dfb\u52a0\u5143\u7d20\u3001\u5faa\u73af\u83b7\u53d6\u7528\u6237\u8f93\u5165\u5e76\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3001\u4f7f\u7528ap [&hellip;]","protected":false},"author":3,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179329"}],"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=1179329"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179329\/revisions"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1179329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1179329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1179329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}