{"id":1088452,"date":"2025-01-08T13:44:20","date_gmt":"2025-01-08T05:44:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1088452.html"},"modified":"2025-01-08T13:44:23","modified_gmt":"2025-01-08T05:44:23","slug":"python%e5%a6%82%e4%bd%95%e5%90%8c%e6%97%b6%e8%be%93%e5%85%a54%e4%b8%aa%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1088452.html","title":{"rendered":"Python\u5982\u4f55\u540c\u65f6\u8f93\u51654\u4e2a\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200915\/a7b55a67-6c32-49d1-8ba3-bd228bcd6ea7.webp\" alt=\"Python\u5982\u4f55\u540c\u65f6\u8f93\u51654\u4e2a\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u540c\u65f6\u8f93\u51654\u4e2a\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u8f93\u5165\u51fd\u6570\u3001\u62c6\u5206\u8f93\u5165\u5b57\u7b26\u4e32\u3001\u4f7f\u7528\u5217\u8868\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u901a\u8fc7<code>input()<\/code>\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\uff0c\u7136\u540e\u4f7f\u7528<code>split()<\/code>\u51fd\u6570\u5c06\u8f93\u5165\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\uff0c\u518d\u5c06\u8fd9\u4e9b\u90e8\u5206\u8f6c\u6362\u4e3a\u6240\u9700\u7684\u6570\u636e\u7c7b\u578b\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u79cd\u65b9\u6cd5\uff0c\u5e76\u5c55\u793a\u5176\u4ed6\u51e0\u79cd\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>input()<\/code> \u548c <code>split()<\/code><\/h3>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u662f\u6700\u5e38\u7528\u7684\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b8c\u6210\u7528\u6237\u8f93\u5165\u7684\u83b7\u53d6\u548c\u5206\u5272\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u83b7\u53d6\u7528\u6237\u8f93\u5165<\/strong>\uff1a\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u6574\u4e2a\u5b57\u7b26\u4e32\u3002<\/li>\n<li><strong>\u5206\u5272\u5b57\u7b26\u4e32<\/strong>\uff1a\u4f7f\u7528<code>split()<\/code>\u51fd\u6570\u5c06\u8f93\u5165\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\u3002<\/li>\n<li><strong>\u8f6c\u6362\u6570\u636e\u7c7b\u578b<\/strong>\uff1a\u5c06\u5206\u5272\u5f97\u5230\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<\/ol>\n<p><p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32<\/p>\n<p>user_input = input(&quot;\u8bf7\u8f93\u51654\u4e2a\u6570\uff0c\u7528\u7a7a\u683c\u5206\u9694\uff1a&quot;)<\/p>\n<h2><strong>\u5206\u5272\u5b57\u7b26\u4e32\u5e76\u8f6c\u6362\u4e3a\u6574\u6570<\/strong><\/h2>\n<p>num1, num2, num3, num4 = map(int, user_input.split())<\/p>\n<p>print(f&quot;\u8f93\u5165\u76844\u4e2a\u6570\u5206\u522b\u662f\uff1a{num1}, {num2}, {num3}, {num4}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u63a7\u5236\u53f0\u4e2d\u8f93\u51654\u4e2a\u7528\u7a7a\u683c\u5206\u9694\u7684\u6570\u5b57\uff0c\u7a0b\u5e8f\u5c06\u8fd9\u4e9b\u6570\u5b57\u8bfb\u53d6\u5e76\u8f6c\u6362\u4e3a\u6574\u6570\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>input()<\/code> \u548c <code>eval()<\/code><\/h3>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u7528\u4e86<code>eval()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u540c\u65f6\u8bfb\u53d6\u591a\u4e2a\u8f93\u5165\u5e76\u81ea\u52a8\u8f6c\u6362\u4e3a\u5408\u9002\u7684\u7c7b\u578b\uff0c\u4f46\u8981\u6ce8\u610f\u5b89\u5168\u95ee\u9898\uff0c\u907f\u514d\u6267\u884c\u6076\u610f\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32<\/p>\n<p>user_input = input(&quot;\u8bf7\u8f93\u51654\u4e2a\u6570\uff0c\u7528\u9017\u53f7\u5206\u9694\uff1a&quot;)<\/p>\n<h2><strong>\u4f7f\u7528 eval() \u51fd\u6570\u8f6c\u6362\u8f93\u5165<\/strong><\/h2>\n<p>num1, num2, num3, num4 = eval(user_input)<\/p>\n<p>print(f&quot;\u8f93\u5165\u76844\u4e2a\u6570\u5206\u522b\u662f\uff1a{num1}, {num2}, {num3}, {num4}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7528\u6237\u8f93\u5165\u7684\u6570\u5b57\u9700\u8981\u7528\u9017\u53f7\u5206\u9694\uff0c\u7a0b\u5e8f\u4f1a\u81ea\u52a8\u5c06\u8f93\u5165\u8f6c\u6362\u4e3a\u6570\u5b57\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u548c\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5904\u7406\u66f4\u591a\u7684\u8f93\u5165\uff0c\u6216\u8005\u9700\u8981\u5bf9\u8f93\u5165\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5904\u7406\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u548c\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u5217\u8868<\/p>\n<p>numbers = []<\/p>\n<h2><strong>\u4f7f\u7528\u5faa\u73af\u83b7\u53d64\u4e2a\u8f93\u5165<\/strong><\/h2>\n<p>for i in range(4):<\/p>\n<p>    number = int(input(f&quot;\u8bf7\u8f93\u5165\u7b2c {i+1} \u4e2a\u6570\uff1a&quot;))<\/p>\n<p>    numbers.append(number)<\/p>\n<p>print(f&quot;\u8f93\u5165\u76844\u4e2a\u6570\u5206\u522b\u662f\uff1a{numbers[0]}, {numbers[1]}, {numbers[2]}, {numbers[3]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u4f9d\u6b21\u63d0\u793a\u7528\u6237\u8f93\u51654\u4e2a\u6570\uff0c\u5e76\u5c06\u8fd9\u4e9b\u6570\u5b58\u50a8\u5728\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u51fd\u6570\u5c01\u88c5\u8f93\u5165\u903b\u8f91<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\uff0c\u53ef\u4ee5\u5c06\u8f93\u5165\u903b\u8f91\u5c01\u88c5\u5230\u4e00\u4e2a\u51fd\u6570\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_four_numbers():<\/p>\n<p>    user_input = input(&quot;\u8bf7\u8f93\u51654\u4e2a\u6570\uff0c\u7528\u7a7a\u683c\u5206\u9694\uff1a&quot;)<\/p>\n<p>    return map(int, user_input.split())<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u83b7\u53d64\u4e2a\u6570<\/strong><\/h2>\n<p>num1, num2, num3, num4 = get_four_numbers()<\/p>\n<p>print(f&quot;\u8f93\u5165\u76844\u4e2a\u6570\u5206\u522b\u662f\uff1a{num1}, {num2}, {num3}, {num4}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u51fd\u6570\u5c01\u88c5\uff0c\u53ef\u4ee5\u5728\u591a\u4e2a\u5730\u65b9\u91cd\u590d\u4f7f\u7528\u8f93\u5165\u903b\u8f91\uff0c\u4fdd\u6301\u4ee3\u7801\u7684\u7b80\u6d01\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528 <code>numpy<\/code> \u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5904\u7406\u9886\u57df\uff0c<code>numpy<\/code> \u5e93\u5e7f\u6cdb\u4f7f\u7528\u3002\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528 <code>numpy<\/code> \u6765\u5904\u7406\u8f93\u5165\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>user_input = input(&quot;\u8bf7\u8f93\u51654\u4e2a\u6570\uff0c\u7528\u7a7a\u683c\u5206\u9694\uff1a&quot;)<\/p>\n<h2><strong>\u4f7f\u7528 numpy \u5904\u7406\u8f93\u5165<\/strong><\/h2>\n<p>numbers = np.fromstring(user_input, sep=&#39; &#39;)<\/p>\n<p>print(f&quot;\u8f93\u5165\u76844\u4e2a\u6570\u5206\u522b\u662f\uff1a{numbers[0]}, {numbers[1]}, {numbers[2]}, {numbers[3]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u4e00\u6b65\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528 <code>argparse<\/code> \u5e93<\/h3>\n<\/p>\n<p><p><code>argparse<\/code> \u5e93\u4e3b\u8981\u7528\u4e8e\u547d\u4ee4\u884c\u53c2\u6570\u89e3\u6790\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u6765\u5904\u7406\u8f93\u5165\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import argparse<\/p>\n<h2><strong>\u521b\u5efa\u89e3\u6790\u5668<\/strong><\/h2>\n<p>parser = argparse.ArgumentParser(description=&quot;\u8f93\u51654\u4e2a\u6570&quot;)<\/p>\n<h2><strong>\u6dfb\u52a0\u53c2\u6570<\/strong><\/h2>\n<p>parser.add_argument(&#39;numbers&#39;, metavar=&#39;N&#39;, type=int, nargs=4, help=&#39;4\u4e2a\u6570&#39;)<\/p>\n<h2><strong>\u89e3\u6790\u53c2\u6570<\/strong><\/h2>\n<p>args = parser.parse_args()<\/p>\n<p>print(f&quot;\u8f93\u5165\u76844\u4e2a\u6570\u5206\u522b\u662f\uff1a{args.numbers[0]}, {args.numbers[1]}, {args.numbers[2]}, {args.numbers[3]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u901a\u8fc7\u547d\u4ee4\u884c\u6267\u884c\u811a\u672c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u5b9e\u73b0Python\u4e2d\u540c\u65f6\u8f93\u51654\u4e2a\u6570\u7684\u9700\u6c42\u3002\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\uff0c\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5904\u7406\u8f93\u5165\u6570\u636e\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c<strong>\u63a8\u8350\u4f7f\u7528<code>input()<\/code>\u548c<code>split()<\/code>\u76f8\u7ed3\u5408\u7684\u65b9\u6cd5<\/strong>\uff0c\u56e0\u4e3a\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u63a5\uff0c\u9002\u7528\u8303\u56f4\u5e7f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u540c\u65f6\u8f93\u5165\u591a\u4e2a\u6570\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u7ed3\u5408<code>split()<\/code>\u65b9\u6cd5\u4e00\u6b21\u6027\u8f93\u5165\u591a\u4e2a\u6570\u503c\u3002\u7528\u6237\u53ef\u4ee5\u5728\u8f93\u5165\u65f6\u7528\u7a7a\u683c\u5206\u9694\u6bcf\u4e2a\u6570\u3002\u4f8b\u5982\uff0c\u7528\u6237\u53ef\u4ee5\u8f93\u5165\u201c1 2 3 4\u201d\uff0c\u7136\u540e\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868\uff1a<\/p>\n<pre><code class=\"language-python\">numbers = list(map(int, input(&quot;\u8bf7\u8f93\u51654\u4e2a\u6570\u5b57\uff0c\u7528\u7a7a\u683c\u5206\u9694\uff1a&quot;).split()))\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u63a5\u6536\u7528\u6237\u8f93\u5165\u65f6\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u8f93\u5165\u4e0d\u7b26\u5408\u9884\u671f\u7684\u60c5\u51b5\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u9519\u8bef\uff0c\u5e76\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">try:\n    numbers = list(map(int, input(&quot;\u8bf7\u8f93\u51654\u4e2a\u6570\u5b57\uff0c\u7528\u7a7a\u683c\u5206\u9694\uff1a&quot;).split()))\n    if len(numbers) != 4:\n        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;\u8bf7\u8f93\u5165\u6b63\u597d4\u4e2a\u6570\u5b57\u3002&quot;)\nexcept ValueError as e:\n    print(f&quot;\u8f93\u5165\u9519\u8bef\uff1a{e}&quot;)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u8f93\u5165\u7684\u6570\u503c\u5728\u7279\u5b9a\u8303\u56f4\u5185\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5bf9\u7528\u6237\u8f93\u5165\u7684\u6570\u503c\u8fdb\u884c\u8303\u56f4\u9650\u5236\uff0c\u53ef\u4ee5\u5728\u8f93\u5165\u540e\u8fdb\u884c\u68c0\u67e5\u3002\u4f8b\u5982\uff0c\u5982\u679c\u8981\u6c42\u8f93\u5165\u7684\u6570\u5b57\u57281\u5230100\u4e4b\u95f4\uff0c\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u6761\u4ef6\u5224\u65ad\uff1a<\/p>\n<pre><code class=\"language-python\">numbers = list(map(int, input(&quot;\u8bf7\u8f93\u51654\u4e2a\u6570\u5b57\uff0c\u7528\u7a7a\u683c\u5206\u9694\uff1a&quot;).split()))\nif all(1 &lt;= num &lt;= 100 for num in numbers):\n    print(&quot;\u8f93\u5165\u6709\u6548\uff01&quot;)\nelse:\n    print(&quot;\u8f93\u5165\u7684\u6570\u5b57\u5fc5\u987b\u57281\u5230100\u4e4b\u95f4\u3002&quot;)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u540c\u65f6\u8f93\u51654\u4e2a\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u8f93\u5165\u51fd\u6570\u3001\u62c6\u5206\u8f93\u5165\u5b57\u7b26\u4e32\u3001\u4f7f\u7528\u5217\u8868\u7b49\u3002 \u5176\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1088462,"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\/1088452"}],"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=1088452"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1088452\/revisions"}],"predecessor-version":[{"id":1088464,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1088452\/revisions\/1088464"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1088462"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1088452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1088452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1088452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}