{"id":1009582,"date":"2024-12-27T11:13:03","date_gmt":"2024-12-27T03:13:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1009582.html"},"modified":"2024-12-27T11:13:07","modified_gmt":"2024-12-27T03:13:07","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%a4%9a%e6%ac%a1%e8%be%93%e5%85%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1009582.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u591a\u6b21\u8f93\u5165"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084444\/0b2bce08-6631-4ebc-b8e4-30bec17d06d9.webp\" alt=\"python\u4e2d\u5982\u4f55\u591a\u6b21\u8f93\u5165\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u8981\u5b9e\u73b0\u591a\u6b21\u8f93\u5165\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u5faa\u73af\u7ed3\u5408<code>input()<\/code>\u51fd\u6570\u3001\u5b9a\u4e49\u81ea\u5b9a\u4e49\u51fd\u6570\u3001\u4f7f\u7528\u5217\u8868\u6216\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u6765\u5b58\u50a8\u8f93\u5165\u503c\u7b49\u3002<strong>\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5faa\u73af\u548c<code>input()<\/code>\u51fd\u6570\u3001\u5b9a\u4e49\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u591a\u6b21\u8f93\u5165<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u4ee3\u7801\u793a\u4f8b\u6765\u5e2e\u52a9\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5faa\u73af\u7ed3\u5408<code>input()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528<code>while<\/code>\u5faa\u73af<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7<code>while<\/code>\u5faa\u73af\uff0c\u53ef\u4ee5\u4e0d\u65ad\u5730\u63d0\u793a\u7528\u6237\u8f93\u5165\uff0c\u76f4\u5230\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u4e3a\u6b62\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u77e5\u9053\u8f93\u5165\u6b21\u6570\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">inputs = []<\/p>\n<p>while True:<\/p>\n<p>    user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u503c\uff08\u8f93\u5165&#39;quit&#39;\u7ed3\u675f\uff09\uff1a&quot;)<\/p>\n<p>    if user_input.lower() == &#39;quit&#39;:<\/p>\n<p>        break<\/p>\n<p>    inputs.append(user_input)<\/p>\n<p>print(&quot;\u60a8\u8f93\u5165\u7684\u503c\u4e3a\uff1a&quot;, inputs)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u4e00\u76f4\u63d0\u793a\u7528\u6237\u8f93\u5165\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165<code>&#39;quit&#39;<\/code>\u3002<strong>\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u53ef\u4ee5\u7075\u6d3b\u5730\u5904\u7406\u7528\u6237\u8f93\u5165\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u5217\u8868\u4e2d<\/strong>\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528<code>for<\/code>\u5faa\u73af<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9884\u5148\u77e5\u9053\u9700\u8981\u8f93\u5165\u7684\u6b21\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">inputs = []<\/p>\n<p>n = int(input(&quot;\u60a8\u60f3\u8f93\u5165\u591a\u5c11\u4e2a\u503c\uff1f\uff1a&quot;))<\/p>\n<p>for _ in range(n):<\/p>\n<p>    user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u503c\uff1a&quot;)<\/p>\n<p>    inputs.append(user_input)<\/p>\n<p>print(&quot;\u60a8\u8f93\u5165\u7684\u503c\u4e3a\uff1a&quot;, inputs)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c<code>for<\/code>\u5faa\u73af\u4f1a\u6839\u636e\u7528\u6237\u6307\u5b9a\u7684\u6b21\u6570\u6765\u6536\u96c6\u8f93\u5165<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b9a\u4e49\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u5904\u7406\u591a\u6b21\u8f93\u5165\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u590d\u7528\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_multiple_inputs(n):<\/p>\n<p>    inputs = []<\/p>\n<p>    for _ in range(n):<\/p>\n<p>        user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u503c\uff1a&quot;)<\/p>\n<p>        inputs.append(user_input)<\/p>\n<p>    return inputs<\/p>\n<p>n = int(input(&quot;\u60a8\u60f3\u8f93\u5165\u591a\u5c11\u4e2a\u503c\uff1f\uff1a&quot;))<\/p>\n<p>result = get_multiple_inputs(n)<\/p>\n<p>print(&quot;\u60a8\u8f93\u5165\u7684\u503c\u4e3a\uff1a&quot;, result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u901a\u8fc7\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u591a\u6b21\u8f93\u5165\u7684\u903b\u8f91\u5c01\u88c5\u8d77\u6765\uff0c\u4fbf\u4e8e\u5728\u4e0d\u540c\u7684\u4e0a\u4e0b\u6587\u4e2d\u91cd\u590d\u4f7f\u7528<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u6216\u5176\u4ed6\u6570\u636e\u7ed3\u6784<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u5217\u8868\u5b58\u50a8\u8f93\u5165<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u662f\u5b58\u50a8\u591a\u6b21\u8f93\u5165\u7684\u5e38\u7528\u6570\u636e\u7ed3\u6784\uff0c\u4f7f\u7528\u5217\u8868\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u8f93\u5165\u7684\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">inputs = []<\/p>\n<p>for _ in range(5):  # \u5047\u8bbe\u8f93\u51655\u6b21<\/p>\n<p>    user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u503c\uff1a&quot;)<\/p>\n<p>    inputs.append(user_input)<\/p>\n<p>print(&quot;\u60a8\u8f93\u5165\u7684\u503c\u4e3a\uff1a&quot;, inputs)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5217\u8868\u63d0\u4f9b\u4e86\u52a8\u6001\u6269\u5c55\u7684\u80fd\u529b\uff0c\u9002\u5408\u5b58\u50a8\u4e0d\u5b9a\u6570\u91cf\u7684\u8f93\u5165\u503c<\/strong>\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u5b57\u5178\u5b58\u50a8\u952e\u503c\u5bf9\u8f93\u5165<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u8f93\u5165\u53ef\u80fd\u9700\u8981\u952e\u503c\u5bf9\u5f62\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u5b58\u50a8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">inputs = {}<\/p>\n<p>for _ in range(3):  # \u5047\u8bbe\u8f93\u51653\u6b21<\/p>\n<p>    key = input(&quot;\u8bf7\u8f93\u5165\u952e\uff1a&quot;)<\/p>\n<p>    value = input(&quot;\u8bf7\u8f93\u5165\u503c\uff1a&quot;)<\/p>\n<p>    inputs[key] = value<\/p>\n<p>print(&quot;\u60a8\u8f93\u5165\u7684\u952e\u503c\u5bf9\u4e3a\uff1a&quot;, inputs)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5b57\u5178\u5141\u8bb8\u4ee5\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u5b58\u50a8\u8f93\u5165\u6570\u636e\uff0c\u9002\u5408\u9700\u8981\u5173\u8054\u8f93\u5165\u7684\u573a\u666f<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u8f93\u5165\u4e2d\u7684\u5e38\u89c1\u95ee\u9898<\/h3>\n<\/p>\n<p><h4>1. \u8f93\u5165\u9a8c\u8bc1<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u9a8c\u8bc1\u8f93\u5165\u662f\u5426\u5408\u6cd5\u3002\u4f8b\u5982\uff0c\u786e\u4fdd\u8f93\u5165\u662f\u6570\u5b57\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\u6570\u5b57\uff1a&quot;)<\/p>\n<p>    if user_input.isdigit():<\/p>\n<p>        break<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u5165\u9a8c\u8bc1\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff0c\u9632\u6b62\u56e0\u65e0\u6548\u8f93\u5165\u5bfc\u81f4\u7684\u9519\u8bef<\/strong>\u3002<\/p>\n<\/p>\n<p><h4>2. \u53bb\u9664\u8f93\u5165\u4e2d\u7684\u7a7a\u767d\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>strip()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u53bb\u9664\u8f93\u5165\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u767d\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u503c\uff1a&quot;).strip()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u53bb\u9664\u7a7a\u767d\u5b57\u7b26\u53ef\u4ee5\u786e\u4fdd\u8f93\u5165\u6570\u636e\u7684\u51c6\u786e\u6027<\/strong>\u3002<\/p>\n<\/p>\n<p><h4>3. \u5904\u7406\u5f02\u5e38\u8f93\u5165<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>try-except<\/code>\u7ed3\u6784\u53ef\u4ee5\u6355\u83b7\u5e76\u5904\u7406\u8f93\u5165\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u53d1\u751f\u7684\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    try:<\/p>\n<p>        user_input = int(input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570\uff1a&quot;))<\/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><\/code><\/pre>\n<\/p>\n<p><p><strong>\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u53ef\u4ee5\u4f18\u96c5\u5730\u6355\u83b7\u5e76\u5904\u7406\u4e0d\u7b26\u5408\u9884\u671f\u7684\u8f93\u5165<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5b9e\u73b0\u591a\u6b21\u8f93\u5165\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u5faa\u73af\u3001\u5b9a\u4e49\u81ea\u5b9a\u4e49\u51fd\u6570\u4ee5\u53ca\u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u6765\u5b58\u50a8\u8f93\u5165\u503c\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002<strong>\u901a\u8fc7\u5bf9\u8f93\u5165\u7684\u9a8c\u8bc1\u548c\u5f02\u5e38\u5904\u7406\uff0c\u53ef\u4ee5\u63d0\u5347\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u7528\u6237\u4f53\u9a8c<\/strong>\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u591a\u6b21\u8f93\u5165\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u6765\u5b9e\u73b0\u591a\u6b21\u8f93\u5165\u3002\u901a\u5e38\u4f7f\u7528<code>while<\/code>\u6216<code>for<\/code>\u5faa\u73af\u6765\u63d0\u793a\u7528\u6237\u8f93\u5165\uff0c\u76f4\u5230\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>while True<\/code>\u6765\u65e0\u9650\u5faa\u73af\u8f93\u5165\uff0c\u76f4\u5230\u7528\u6237\u9009\u62e9\u9000\u51fa\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u6709\u6548\u6027\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u6709\u6548\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u5f02\u5e38\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u9a8c\u8bc1\u8f93\u5165\u662f\u5426\u7b26\u5408\u9884\u671f\u683c\u5f0f\uff0c\u6bd4\u5982\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u6216\u7279\u5b9a\u7684\u5b57\u7b26\u3002\u540c\u65f6\uff0c\u8fd8\u53ef\u4ee5\u5728\u8f93\u5165\u4e0d\u7b26\u5408\u8981\u6c42\u65f6\uff0c\u7ed9\u51fa\u63d0\u793a\u4fe1\u606f\uff0c\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002<\/p>\n<p><strong>\u6709\u4ec0\u4e48\u65b9\u6cd5\u53ef\u4ee5\u6279\u91cf\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u5c06\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u5b58\u50a8\u5728\u5217\u8868\u6216\u5b57\u5178\u4e2d\uff0c\u4ee5\u4fbf\u8fdb\u884c\u6279\u91cf\u5904\u7406\u3002\u4f7f\u7528\u5faa\u73af\u63a5\u6536\u8f93\u5165\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u4e00\u4e2a\u6570\u636e\u7ed3\u6784\u4e2d\u3002\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u540e\u7eed\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u6392\u5e8f\u3001\u7edf\u8ba1\u7b49\uff0c\u6781\u5927\u63d0\u9ad8\u4e86\u6570\u636e\u5904\u7406\u7684\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8981\u5b9e\u73b0\u591a\u6b21\u8f93\u5165\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u5faa\u73af\u7ed3\u5408input()\u51fd\u6570\u3001\u5b9a\u4e49\u81ea\u5b9a\u4e49\u51fd\u6570\u3001\u4f7f\u7528\u5217\u8868\u6216 [&hellip;]","protected":false},"author":3,"featured_media":1009596,"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\/1009582"}],"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=1009582"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1009582\/revisions"}],"predecessor-version":[{"id":1009597,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1009582\/revisions\/1009597"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1009596"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1009582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1009582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1009582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}