{"id":1071414,"date":"2025-01-08T11:07:06","date_gmt":"2025-01-08T03:07:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1071414.html"},"modified":"2025-01-08T11:07:09","modified_gmt":"2025-01-08T03:07:09","slug":"python-%e5%a6%82%e4%bd%95%e8%bf%90%e8%a1%8c%e6%97%b6%e8%be%93%e5%85%a5%e5%86%85%e5%ae%b9-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1071414.html","title":{"rendered":"python \u5982\u4f55\u8fd0\u884c\u65f6\u8f93\u5165\u5185\u5bb9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101932\/7217459e-c641-4c66-bce5-053d0272f181.webp\" alt=\"python \u5982\u4f55\u8fd0\u884c\u65f6\u8f93\u5165\u5185\u5bb9\" \/><\/p>\n<p><p> <strong>Python \u8fd0\u884c\u65f6\u8f93\u5165\u5185\u5bb9\u7684\u65b9\u5f0f\u4e3b\u8981\u6709\uff1a\u4f7f\u7528 <code>input()<\/code> \u51fd\u6570\u3001\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u4f20\u9012\u3001\u4ee5\u53ca\u4f7f\u7528\u6587\u4ef6\u8f93\u5165\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u89c1\u4e14\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 <code>input()<\/code> \u51fd\u6570\u3002<code>input()<\/code> \u51fd\u6570\u5141\u8bb8\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u7b49\u5f85\u7528\u6237\u8f93\u5165\uff0c\u5e76\u5c06\u8f93\u5165\u7684\u5185\u5bb9\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fd4\u56de\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>input()<\/code> \u51fd\u6570<\/h3>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 <code>input()<\/code> \u51fd\u6570\u662f\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c\u5b83\u4f1a\u6682\u505c\u7a0b\u5e8f\u7684\u6267\u884c\uff0c\u7b49\u5f85\u7528\u6237\u8f93\u5165\uff0c\u7136\u540e\u8fd4\u56de\u8f93\u5165\u7684\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = input(&quot;Enter your name: &quot;)<\/p>\n<p>print(f&quot;Hello, {name}!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u63d0\u793a\u7528\u6237\u8f93\u5165\u4ed6\u4eec\u7684\u540d\u5b57\uff0c\u7136\u540e\u6253\u5370\u4e00\u6761\u95ee\u5019\u8bed\u3002<\/p>\n<\/p>\n<p><h4>2. \u8f93\u5165\u6570\u5b57<\/h4>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>input()<\/code> \u51fd\u6570\u8fd4\u56de\u7684\u6570\u636e\u7c7b\u578b\u662f\u5b57\u7b26\u4e32\u3002\u5982\u679c\u9700\u8981\u5904\u7406\u6570\u503c\u8f93\u5165\uff0c\u53ef\u4ee5\u4f7f\u7528\u7c7b\u578b\u8f6c\u6362\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">age = int(input(&quot;Enter your age: &quot;))<\/p>\n<p>print(f&quot;You are {age} years old.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>int()<\/code> \u51fd\u6570\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u4f20\u9012<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528 <code>sys.argv<\/code><\/h4>\n<\/p>\n<p><p>Python \u7684 <code>sys<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a <code>argv<\/code> \u7684\u5217\u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u547d\u4ee4\u884c\u53c2\u6570\u3002<code>argv<\/code> \u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u811a\u672c\u7684\u540d\u79f0\uff0c\u540e\u9762\u7684\u5143\u7d20\u662f\u4f20\u9012\u7ed9\u811a\u672c\u7684\u53c2\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>if len(sys.argv) &lt; 2:<\/p>\n<p>    print(&quot;Usage: python script.py &lt;your_name&gt;&quot;)<\/p>\n<p>    sys.exit(1)<\/p>\n<p>name = sys.argv[1]<\/p>\n<p>print(f&quot;Hello, {name}!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u8fd9\u4e2a\u811a\u672c\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u4f20\u9012\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python script.py Alice<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello, Alice!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528 <code>argparse<\/code> \u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u547d\u4ee4\u884c\u53c2\u6570\u89e3\u6790\uff0cPython \u63d0\u4f9b\u4e86 <code>argparse<\/code> \u6a21\u5757\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 <code>argparse<\/code> \u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import argparse<\/p>\n<p>parser = argparse.ArgumentParser(description=&quot;Greet the user.&quot;)<\/p>\n<p>parser.add_argument(&quot;name&quot;, type=str, help=&quot;The name of the user.&quot;)<\/p>\n<p>args = parser.parse_args()<\/p>\n<p>print(f&quot;Hello, {args.name}!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u8fd9\u4e2a\u811a\u672c\u65f6\uff0c\u53ef\u4ee5\u4f20\u9012\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python script.py Bob<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello, Bob!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u6587\u4ef6\u8f93\u5165<\/h3>\n<\/p>\n<p><h4>1. \u4ece\u6587\u4ef6\u8bfb\u53d6\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u8f93\u5165\u6570\u636e\u53ef\u80fd\u5b58\u50a8\u5728\u6587\u4ef6\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528 <code>open()<\/code> \u51fd\u6570\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u8bfb\u53d6\u5176\u5185\u5bb9\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;input.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u8bfb\u53d6 <code>input.txt<\/code> \u6587\u4ef6\u7684\u5185\u5bb9\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h4>2. \u9010\u884c\u8bfb\u53d6<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u6587\u4ef6\u5185\u5bb9\u8f83\u5927\uff0c\u53ef\u4ee5\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;input.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        print(line.strip())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4f1a\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u6253\u5370\u6bcf\u4e00\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u6807\u51c6\u8f93\u5165\u6d41<\/h3>\n<\/p>\n<p><p>\u9664\u4e86 <code>input()<\/code> \u51fd\u6570\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6807\u51c6\u8f93\u5165\u6d41 <code>sys.stdin<\/code> \u8fdb\u884c\u8f93\u5165\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>print(&quot;Enter some text:&quot;)<\/p>\n<p>for line in sys.stdin:<\/p>\n<p>    if line.strip() == &quot;exit&quot;:<\/p>\n<p>        break<\/p>\n<p>    print(f&quot;You entered: {line.strip()}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4f1a\u4e00\u76f4\u8bfb\u53d6\u7528\u6237\u8f93\u5165\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165 <code>exit<\/code> \u4e3a\u6b62\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>Python \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u5728\u8fd0\u884c\u65f6\u8f93\u5165\u5185\u5bb9<\/strong>\uff0c\u5305\u62ec\u4f7f\u7528 <code>input()<\/code> \u51fd\u6570\u3001\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u4f20\u9012\u3001\u4ee5\u53ca\u4f7f\u7528\u6587\u4ef6\u8f93\u5165\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u4f7f\u7528 <code>input()<\/code> \u51fd\u6570\u662f\u6700\u5e38\u89c1\u4e14\u6700\u76f4\u63a5\u7684\u65b9\u6cd5<\/strong>\uff0c\u9002\u5408\u9700\u8981\u7b80\u5355\u4ea4\u4e92\u7684\u7a0b\u5e8f\u3002<\/p>\n<p><strong>2. \u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u4f20\u9012\u5219\u9002\u5408\u9700\u8981\u4ece\u5916\u90e8\u4f20\u9012\u53c2\u6570\u7684\u811a\u672c<\/strong>\uff0c\u5c24\u5176\u662f\u9700\u8981\u81ea\u52a8\u5316\u8fd0\u884c\u7684\u811a\u672c\u3002<\/p>\n<p><strong>3. \u4f7f\u7528\u6587\u4ef6\u8f93\u5165\u9002\u5408\u5904\u7406\u5927\u91cf\u6570\u636e\u7684\u60c5\u51b5<\/strong>\uff0c\u6587\u4ef6\u53ef\u4ee5\u5b58\u50a8\u5927\u89c4\u6a21\u7684\u8f93\u5165\u6570\u636e\uff0c\u65b9\u4fbf\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u638c\u63e1\u8fd9\u4e9b\u8f93\u5165\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8 Python \u7a0b\u5e8f\u7684\u7075\u6d3b\u6027\u548c\u5b9e\u7528\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u7528\u6237\u8f93\u5165\u7684\u529f\u80fd\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>input()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u7528\u6237\u7684\u8f93\u5165\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u6682\u505c\u7a0b\u5e8f\u7684\u6267\u884c\uff0c\u7b49\u5f85\u7528\u6237\u8f93\u5165\u5185\u5bb9\u3002\u5f53\u7528\u6237\u8f93\u5165\u6570\u636e\u5e76\u6309\u4e0b\u56de\u8f66\u952e\u540e\uff0c\u8f93\u5165\u7684\u5185\u5bb9\u5c06\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fd4\u56de\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e9b\u5185\u5bb9\uff1a&quot;)\nprint(&quot;\u60a8\u8f93\u5165\u7684\u5185\u5bb9\u662f\uff1a&quot;, user_input)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u9002\u7528\u4e8e\u83b7\u53d6\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u8f93\u5165\uff0c\u5982\u679c\u9700\u8981\u5c06\u8f93\u5165\u8f6c\u6362\u4e3a\u5176\u4ed6\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5e94\u7684\u8f6c\u6362\u51fd\u6570\uff0c\u5982<code>int()<\/code>\u6216<code>float()<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7c7b\u578b\u9519\u8bef\u6216\u683c\u5f0f\u4e0d\u6b63\u786e\u7684\u60c5\u51b5\u3002\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u5f02\u5e38\u5e76\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">while True:\n    try:\n        user_input = int(input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570\uff1a&quot;))\n        break  # \u5982\u679c\u8f93\u5165\u6210\u529f\uff0c\u8df3\u51fa\u5faa\u73af\n    except ValueError:\n        print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u786e\u4fdd\u7a0b\u5e8f\u4e0d\u4f1a\u56e0\u4e3a\u9519\u8bef\u8f93\u5165\u800c\u5d29\u6e83\uff0c\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u9650\u5236\u7528\u6237\u8f93\u5165\u7684\u8303\u56f4\uff1f<\/strong><br \/>\u82e5\u5e0c\u671b\u9650\u5236\u7528\u6237\u8f93\u5165\u7684\u8303\u56f4\uff0c\u53ef\u4ee5\u5728\u8f93\u5165\u540e\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\u3002\u4f8b\u5982\uff0c\u5982\u679c\u9700\u8981\u7528\u6237\u8f93\u5165\u4e00\u4e2a1\u523010\u4e4b\u95f4\u7684\u6574\u6570\uff0c\u53ef\u4ee5\u8fd9\u6837\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">while True:\n    user_input = int(input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a1\u523010\u4e4b\u95f4\u7684\u6574\u6570\uff1a&quot;))\n    if 1 &lt;= user_input &lt;= 10:\n        print(&quot;\u60a8\u8f93\u5165\u7684\u6570\u5b57\u662f\uff1a&quot;, user_input)\n        break\n    else:\n        print(&quot;\u8f93\u5165\u4e0d\u5728\u8303\u56f4\u5185\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u6709\u6548\u5730\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u7b26\u5408\u9884\u671f\u7684\u8303\u56f4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u8fd0\u884c\u65f6\u8f93\u5165\u5185\u5bb9\u7684\u65b9\u5f0f\u4e3b\u8981\u6709\uff1a\u4f7f\u7528 input() \u51fd\u6570\u3001\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u4f20\u9012\u3001\u4ee5\u53ca\u4f7f\u7528\u6587\u4ef6\u8f93\u5165\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1071422,"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\/1071414"}],"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=1071414"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1071414\/revisions"}],"predecessor-version":[{"id":1071425,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1071414\/revisions\/1071425"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1071422"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1071414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1071414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1071414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}