{"id":1060707,"date":"2024-12-31T15:36:20","date_gmt":"2024-12-31T07:36:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1060707.html"},"modified":"2024-12-31T15:36:23","modified_gmt":"2024-12-31T07:36:23","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e4%b8%a4%e4%b8%aainput","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1060707.html","title":{"rendered":"Python\u4e2d\u5982\u4f55\u4f7f\u7528\u4e24\u4e2ainput"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/bd50d140-8953-4999-88a9-d3b5aff39208.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"Python\u4e2d\u5982\u4f55\u4f7f\u7528\u4e24\u4e2ainput\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u4e24\u4e2ainput\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u4e24\u6b21input()\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\u5206\u522b\u8c03\u7528\u4e24\u6b21input()\u51fd\u6570\u3001\u5728\u4e00\u884c\u4e2d\u8f93\u5165\u4e24\u4e2a\u503c\u5e76\u62c6\u5206\u3001\u4ee5\u53ca\u901a\u8fc7\u5217\u8868\u6216\u5b57\u5178\u6765\u5b58\u50a8\u591a\u4e2a\u8f93\u5165\u3002<\/strong>\u5e38\u89c1\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u5206\u522b\u8c03\u7528\u4e24\u6b21input()\u51fd\u6570\u3001\u5728\u4e00\u884c\u4e2d\u8f93\u5165\u591a\u4e2a\u503c\u5e76\u62c6\u5206\u3001\u4ee5\u53ca\u4f7f\u7528\u5217\u8868\u6216\u5b57\u5178\u6765\u5b58\u50a8\u8f93\u5165\u6570\u636e\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5206\u522b\u8c03\u7528\u4e24\u6b21input()\u51fd\u6570<\/h3>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>Python\u7684input()\u51fd\u6570\u7528\u4e8e\u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u53d6\u4e00\u884c\u6587\u672c\uff0c\u8fd4\u56de\u503c\u662f\u5b57\u7b26\u4e32\u3002\u6211\u4eec\u53ef\u4ee5\u5206\u522b\u8c03\u7528\u4e24\u6b21input()\u51fd\u6570\u6765\u83b7\u53d6\u4e24\u4e2a\u8f93\u5165\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">input1 = input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e00\u4e2a\u503c: &quot;)<\/p>\n<p>input2 = input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e8c\u4e2a\u503c: &quot;)<\/p>\n<p>print(&quot;\u7b2c\u4e00\u4e2a\u503c\u662f:&quot;, input1)<\/p>\n<p>print(&quot;\u7b2c\u4e8c\u4e2a\u503c\u662f:&quot;, input2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u6700\u4e3a\u76f4\u89c2\uff0c\u6bcf\u6b21\u8c03\u7528input()\u51fd\u6570\u90fd\u4f1a\u7b49\u5f85\u7528\u6237\u8f93\u5165\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5176\u4ed6\u6570\u636e\u7c7b\u578b\uff0c\u4f8b\u5982\u6574\u6570\u6216\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h4>2. \u8f6c\u6362\u6570\u636e\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u8f93\u5165\u7684\u6570\u636e\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\uff0c\u4f8b\u5982\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528int()\u6216float()\u51fd\u6570\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num1 = int(input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e00\u4e2a\u6574\u6570: &quot;))<\/p>\n<p>num2 = float(input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e8c\u4e2a\u6d6e\u70b9\u6570: &quot;))<\/p>\n<p>print(&quot;\u7b2c\u4e00\u4e2a\u6574\u6570\u662f:&quot;, num1)<\/p>\n<p>print(&quot;\u7b2c\u4e8c\u4e2a\u6d6e\u70b9\u6570\u662f:&quot;, num2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5728\u4e00\u884c\u4e2d\u8f93\u5165\u591a\u4e2a\u503c\u5e76\u62c6\u5206<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528split()\u65b9\u6cd5\u62c6\u5206\u8f93\u5165<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u5e0c\u671b\u7528\u6237\u5728\u4e00\u884c\u4e2d\u8f93\u5165\u591a\u4e2a\u503c\uff0c\u5e76\u5bf9\u8fd9\u4e9b\u503c\u8fdb\u884c\u62c6\u5206\u5904\u7406\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528split()\u65b9\u6cd5\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u62c6\u5206\u4e3a\u591a\u4e2a\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">inputs = input(&quot;\u8bf7\u8f93\u5165\u4e24\u4e2a\u503c\uff0c\u5e76\u7528\u7a7a\u683c\u5206\u9694: &quot;)<\/p>\n<p>input1, input2 = inputs.split()<\/p>\n<p>print(&quot;\u7b2c\u4e00\u4e2a\u503c\u662f:&quot;, input1)<\/p>\n<p>print(&quot;\u7b2c\u4e8c\u4e2a\u503c\u662f:&quot;, input2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u62c6\u5206\u5e76\u8f6c\u6362\u6570\u636e\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u62c6\u5206\u8f93\u5165\u7684\u57fa\u7840\u4e0a\uff0c\u5bf9\u62c6\u5206\u540e\u7684\u6570\u636e\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">inputs = input(&quot;\u8bf7\u8f93\u5165\u4e24\u4e2a\u6574\u6570\uff0c\u5e76\u7528\u7a7a\u683c\u5206\u9694: &quot;)<\/p>\n<p>num1, num2 = map(int, inputs.split())<\/p>\n<p>print(&quot;\u7b2c\u4e00\u4e2a\u6574\u6570\u662f:&quot;, num1)<\/p>\n<p>print(&quot;\u7b2c\u4e8c\u4e2a\u6574\u6570\u662f:&quot;, num2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u6216\u5b57\u5178\u5b58\u50a8\u591a\u4e2a\u8f93\u5165<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u5217\u8868\u5b58\u50a8\u8f93\u5165<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5c06\u591a\u4e2a\u8f93\u5165\u5b58\u50a8\u5728\u5217\u8868\u4e2d\uff0c\u4fbf\u4e8e\u540e\u7eed\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">input_list = []<\/p>\n<p>input_list.append(input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e00\u4e2a\u503c: &quot;))<\/p>\n<p>input_list.append(input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e8c\u4e2a\u503c: &quot;))<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u503c\u5206\u522b\u662f:&quot;, input_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u5b57\u5178\u5b58\u50a8\u8f93\u5165<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u5b58\u50a8\u591a\u4e2a\u8f93\u5165\uff0c\u65b9\u4fbf\u6839\u636e\u952e\u540d\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">input_dict = {}<\/p>\n<p>input_dict[&#39;input1&#39;] = input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e00\u4e2a\u503c: &quot;)<\/p>\n<p>input_dict[&#39;input2&#39;] = input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e8c\u4e2a\u503c: &quot;)<\/p>\n<p>print(&quot;\u7b2c\u4e00\u4e2a\u503c\u662f:&quot;, input_dict[&#39;input1&#39;])<\/p>\n<p>print(&quot;\u7b2c\u4e8c\u4e2a\u503c\u662f:&quot;, input_dict[&#39;input2&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7efc\u5408\u5e94\u7528\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><h4>1. \u5b9e\u73b0\u7b80\u5355\u7684\u8ba1\u7b97\u5668<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u8ba1\u7b97\u5668\uff0c\u7528\u6237\u8f93\u5165\u4e24\u4e2a\u6570\u503c\u548c\u4e00\u4e2a\u64cd\u4f5c\u7b26\uff0c\u7a0b\u5e8f\u8f93\u51fa\u8ba1\u7b97\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculator():<\/p>\n<p>    num1 = float(input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e00\u4e2a\u6570: &quot;))<\/p>\n<p>    operator = input(&quot;\u8bf7\u8f93\u5165\u64cd\u4f5c\u7b26 (+, -, *, \/): &quot;)<\/p>\n<p>    num2 = float(input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e8c\u4e2a\u6570: &quot;))<\/p>\n<p>    if operator == &#39;+&#39;:<\/p>\n<p>        result = num1 + num2<\/p>\n<p>    elif operator == &#39;-&#39;:<\/p>\n<p>        result = num1 - num2<\/p>\n<p>    elif operator == &#39;*&#39;:<\/p>\n<p>        result = num1 * num2<\/p>\n<p>    elif operator == &#39;\/&#39;:<\/p>\n<p>        if num2 != 0:<\/p>\n<p>            result = num1 \/ num2<\/p>\n<p>        else:<\/p>\n<p>            result = &quot;\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6&quot;<\/p>\n<p>    else:<\/p>\n<p>        result = &quot;\u65e0\u6548\u7684\u64cd\u4f5c\u7b26&quot;<\/p>\n<p>    print(&quot;\u8ba1\u7b97\u7ed3\u679c\u662f:&quot;, result)<\/p>\n<p>calculator()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u5217\u8868\u548c\u5b57\u5178\u5b9e\u73b0\u6279\u91cf\u8f93\u5165<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u548c\u5b57\u5178\u5b9e\u73b0\u6279\u91cf\u8f93\u5165\uff0c\u7528\u6237\u8f93\u5165\u591a\u4e2a\u503c\uff0c\u7a0b\u5e8f\u5c06\u8fd9\u4e9b\u503c\u5b58\u50a8\u5728\u5217\u8868\u6216\u5b57\u5178\u4e2d\uff0c\u4fbf\u4e8e\u540e\u7eed\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def batch_input():<\/p>\n<p>    num_values = int(input(&quot;\u8bf7\u8f93\u5165\u8981\u8f93\u5165\u7684\u503c\u7684\u6570\u91cf: &quot;))<\/p>\n<p>    input_list = []<\/p>\n<p>    for i in range(num_values):<\/p>\n<p>        value = input(f&quot;\u8bf7\u8f93\u5165\u7b2c{i+1}\u4e2a\u503c: &quot;)<\/p>\n<p>        input_list.append(value)<\/p>\n<p>    print(&quot;\u8f93\u5165\u7684\u503c\u5206\u522b\u662f:&quot;, input_list)<\/p>\n<p>batch_input()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u5728Python\u4e2d\u4f7f\u7528\u4e24\u4e2a\u6216\u591a\u4e2ainput()\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u8f93\u5165\u65b9\u5f0f\uff0c\u786e\u4fdd\u8f93\u5165\u6570\u636e\u7684\u6b63\u786e\u6027\u548c\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u540c\u65f6\u83b7\u53d6\u4e24\u4e2a\u8f93\u5165\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u8fde\u7eed\u8c03\u7528<code>input()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u4e24\u4e2a\u8f93\u5165\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u9996\u5148\u83b7\u53d6\u7b2c\u4e00\u4e2a\u8f93\u5165\uff0c\u7136\u540e\u5728\u83b7\u53d6\u7b2c\u4e8c\u4e2a\u8f93\u5165\u3002\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">input1 = input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e00\u4e2a\u503c\uff1a&quot;)\ninput2 = input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e8c\u4e2a\u503c\uff1a&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u7a0b\u5e8f\u4f1a\u4f9d\u6b21\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e24\u4e2a\u503c\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u5728\u4e00\u884c\u4e2d\u83b7\u53d6\u4e24\u4e2a\u8f93\u5165\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\u5c06\u7528\u6237\u5728\u4e00\u884c\u4e2d\u8f93\u5165\u7684\u591a\u4e2a\u503c\u5206\u5f00\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a  <\/p>\n<pre><code class=\"language-python\">input1, input2 = input(&quot;\u8bf7\u8f93\u5165\u4e24\u4e2a\u503c\uff0c\u7528\u7a7a\u683c\u5206\u9694\uff1a&quot;).split()\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u7528\u6237\u53ea\u9700\u5728\u4e00\u884c\u4e2d\u8f93\u5165\u4e24\u4e2a\u503c\uff0c\u7a0b\u5e8f\u5c31\u4f1a\u5c06\u5b83\u4eec\u5206\u522b\u8d4b\u7ed9<code>input1<\/code>\u548c<code>input2<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u6709\u6548\u6027\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u6709\u6548\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u5bf9\u8f93\u5165\u8fdb\u884c\u9a8c\u8bc1\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u68c0\u67e5\u8f93\u5165\u662f\u5426\u4e3a\u7a7a\uff0c\u6216\u8005\u662f\u5426\u7b26\u5408\u7279\u5b9a\u683c\u5f0f\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">input1 = input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e00\u4e2a\u503c\uff1a&quot;)\nif not input1:\n    print(&quot;\u8f93\u5165\u4e0d\u80fd\u4e3a\u7a7a\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\u3002&quot;)\nelse:\n    input2 = input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e8c\u4e2a\u503c\uff1a&quot;)\n    if not input2:\n        print(&quot;\u8f93\u5165\u4e0d\u80fd\u4e3a\u7a7a\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u7684\u4ee3\u7801\u53ef\u4ee5\u6709\u6548\u5730\u5f15\u5bfc\u7528\u6237\u8fdb\u884c\u6709\u6548\u8f93\u5165\uff0c\u589e\u5f3a\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4f7f\u7528\u4e24\u4e2ainput\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u4e24\u6b21input()\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\u5206\u522b\u8c03\u7528\u4e24\u6b21inpu [&hellip;]","protected":false},"author":3,"featured_media":1060711,"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\/1060707"}],"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=1060707"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1060707\/revisions"}],"predecessor-version":[{"id":1060713,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1060707\/revisions\/1060713"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1060711"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1060707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1060707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1060707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}