{"id":177058,"date":"2024-05-08T19:22:53","date_gmt":"2024-05-08T11:22:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/177058.html"},"modified":"2024-05-08T19:22:56","modified_gmt":"2024-05-08T11:22:56","slug":"python%e8%be%93%e5%85%a5%e4%b8%80%e7%b3%bb%e5%88%97%e6%95%b0%e5%ad%97%ef%bc%8c%e5%b9%b6%e6%b1%82%e5%92%8c%e4%b8%8e%e6%b1%82%e5%b9%b3%e5%9d%87%e6%95%b0%e6%80%8e%e4%b9%88%e5%81%9a","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/177058.html","title":{"rendered":"python\u8f93\u5165\u4e00\u7cfb\u5217\u6570\u5b57\uff0c\u5e76\u6c42\u548c\u4e0e\u6c42\u5e73\u5747\u6570\u600e\u4e48\u505a"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27060851\/872fa056-8985-479b-a073-fab3e1e17ae0.webp\" alt=\"python\u8f93\u5165\u4e00\u7cfb\u5217\u6570\u5b57\uff0c\u5e76\u6c42\u548c\u4e0e\u6c42\u5e73\u5747\u6570\u600e\u4e48\u505a\" \/><\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8f93\u5165\u4e00\u7cfb\u5217\u6570\u5b57\u5e76\u8ba1\u7b97\u5b83\u4eec\u7684\u548c\u4e0e\u5e73\u5747\u6570\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>input()<\/code>\u6765\u63a5\u6536\u8f93\u5165\uff0c\u7136\u540e\u4f7f\u7528\u5faa\u73af\u6216\u5185\u7f6e\u51fd\u6570\u5982<code>sum()<\/code>\u6765\u8ba1\u7b97\u603b\u548c\uff0c\u5e76\u901a\u8fc7\u7b80\u5355\u7684\u7b97\u672f\u64cd\u4f5c\u6765\u8ba1\u7b97\u5e73\u5747\u6570\u3002<strong>\u6838\u5fc3\u6b65\u9aa4\u5305\u62ec\uff1a\u63a5\u6536\u8f93\u5165\u3001\u8f6c\u6362\u6570\u636e\u7c7b\u578b\u3001\u8ba1\u7b97\u603b\u548c\u3001\u8ba1\u7b97\u5e73\u5747\u6570<\/strong>\u3002\u8ba9\u6211\u4eec\u8be6\u7ec6\u5c55\u5f00\u5173\u4e8e<strong>\u63a5\u6536\u8f93\u5165<\/strong>\u7684\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p>\u7528\u6237\u53ef\u4ee5\u901a\u8fc7<code>input()<\/code>\u51fd\u6570\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\u5e8f\u5217\uff0c\u901a\u5e38\u6211\u4eec\u4f7f\u7528\u9017\u53f7 <code>,<\/code> \u6216\u8005\u7a7a\u683c\u4f5c\u4e3a\u6570\u5b57\u4e4b\u95f4\u7684\u5206\u9694\u7b26\u3002\u8f93\u5165\u63a5\u6536\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\u6765\u6309\u7167\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u4e00\u4e2a\u5217\u8868\u3002\u63a5\u4e0b\u6765\uff0c\u901a\u8fc7\u904d\u5386\u8fd9\u4e2a\u5217\u8868\uff0c\u5e76\u4f7f\u7528<code>int()<\/code>\u6216<code>float()<\/code>\u51fd\u6570\u5c06\u6bcf\u4e00\u4e2a\u5b57\u7b26\u4e32\u5143\u7d20\u8f6c\u6362\u6210\u6570\u5b57\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u7531\u6570\u5b57\u7ec4\u6210\u7684\u5217\u8868\uff0c\u4e3a\u540e\u7eed\u7684\u8ba1\u7b97\u505a\u597d\u51c6\u5907\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u63a5\u6536\u5e76\u5904\u7406\u8f93\u5165<\/h2>\n<\/p>\n<p><p>\u4e3a\u4e86\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u4e00\u7cfb\u5217\u6570\u5b57\uff0c\u9996\u5148\u9700\u8981\u63d0\u793a\u7528\u6237\u8f93\u5165\u3002\u53ef\u4ee5\u4f7f\u7528\u9017\u53f7\u6216\u7a7a\u683c\u4f5c\u4e3a\u5206\u9694\u7b26\uff0c\u7136\u540e\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\u5206\u5272\u8fd9\u4e9b\u6570\u5b57\u3002\u63a5\u4e0b\u6765\uff0c\u9700\u8981\u5c06\u5206\u5272\u5f97\u5230\u7684\u5b57\u7b26\u4e32\u5217\u8868\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570\u5217\u8868\uff0c\u56e0\u4e3a\u521d\u59cb\u7684\u8f93\u5165\u662f\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u63a5\u6536\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u7cfb\u5217\u6570\u5b57\uff0c\u4f7f\u7528\u9017\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26<\/p>\n<p>numbers_str = input(&quot;\u8bf7\u8f93\u5165\u4e00\u7cfb\u5217\u6570\u5b57\uff0c\u4f7f\u7528\u9017\u53f7\u5206\u9694\u5b83\u4eec: &quot;)<\/p>\n<h2><strong>\u4f7f\u7528split\u65b9\u6cd5\u6309\u9017\u53f7\u5206\u5272\u5b57\u7b26\u4e32\uff0c\u5f97\u5230\u5b57\u7b26\u4e32\u5217\u8868<\/strong><\/h2>\n<p>numbers_list_str = numbers_str.split(&#039;,&#039;)<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8f6c\u6362\u6bcf\u4e2a\u5b57\u7b26\u4e32\u4e3a\u6574\u6570<\/strong><\/h2>\n<p>numbers = [float(num) for num in numbers_list_str]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u8ba1\u7b97\u603b\u548c<\/h2>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\u8ba1\u7b97\u6570\u5b57\u5217\u8868\u7684\u603b\u548c\u3002Python\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570<code>sum()<\/code>\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\uff0c\u975e\u5e38\u65b9\u4fbf\u5730\u8ba1\u7b97\u51fa\u5217\u8868\u4e2d\u6240\u6709\u6570\u5b57\u7684\u603b\u548c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u6570\u5b57\u5217\u8868\u7684\u603b\u548c<\/p>\n<p>total = sum(numbers)<\/p>\n<p>print(&quot;\u6570\u5b57\u7684\u603b\u548c\u662f:&quot;, total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u8ba1\u7b97\u5e73\u5747\u6570<\/h2>\n<\/p>\n<p><p>\u5f97\u5230\u603b\u548c\u4e4b\u540e\uff0c\u8ba1\u7b97\u5e73\u5747\u6570\u5c31\u7b80\u5355\u4e86\u3002\u53ea\u9700\u4f7f\u7528\u603b\u548c\u9664\u4ee5\u6570\u5b57\u7684\u4e2a\u6570\u3002\u5217\u8868\u5bf9\u8c61\u6709\u4e00\u4e2a\u5185\u7f6e\u65b9\u6cd5<code>len()<\/code>\uff0c\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de\u5217\u8868\u7684\u957f\u5ea6\uff0c\u5373\u6570\u5b57\u7684\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u5e73\u5747\u6570<\/p>\n<p>average = total \/ len(numbers)<\/p>\n<p>print(&quot;\u6570\u5b57\u7684\u5e73\u5747\u6570\u662f:&quot;, average)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b<\/h2>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u5c06\u4e0a\u8ff0\u6b65\u9aa4\u6574\u5408\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u7a0b\u5e8f\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u7cfb\u5217\u6570\u5b57\uff0c\u5e76\u4f7f\u7528\u9017\u53f7\u5206\u9694<\/p>\n<p>numbers_str = input(&quot;\u8bf7\u8f93\u5165\u4e00\u7cfb\u5217\u6570\u5b57\uff0c\u4f7f\u7528\u9017\u53f7\u5206\u9694\u5b83\u4eec: &quot;)<\/p>\n<h2><strong>\u5206\u5272\u5b57\u7b26\u4e32\uff0c\u8f6c\u6362\u4e3a\u6570\u5b57\u5217\u8868<\/strong><\/h2>\n<p>numbers_list_str = numbers_str.split(&#039;,&#039;)<\/p>\n<p>numbers = [float(num) for num in numbers_list_str]<\/p>\n<h2><strong>\u8ba1\u7b97\u603b\u548c\u4e0e\u5e73\u5747\u6570<\/strong><\/h2>\n<p>total = sum(numbers)<\/p>\n<p>average = total \/ len(numbers)<\/p>\n<h2><strong>\u6253\u5370\u7ed3\u679c<\/strong><\/h2>\n<p>print(&quot;\u6570\u5b57\u7684\u603b\u548c\u662f:&quot;, total)<\/p>\n<p>print(&quot;\u6570\u5b57\u7684\u5e73\u5747\u6570\u662f:&quot;, average)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c55\u73b0\u4e86\u5982\u4f55<strong>\u63a5\u6536\u7528\u6237\u8f93\u5165\u7684\u4e00\u7cfb\u5217\u6570\u5b57\u3001\u8f6c\u6362\u6570\u636e\u7c7b\u578b\uff0c\u4ee5\u53ca\u8ba1\u7b97\u8fd9\u4e9b\u6570\u5b57\u7684\u603b\u548c\u548c\u5e73\u5747\u6570<\/strong>\u3002\u7528\u6237\u9700\u8981\u6ce8\u610f\u7684\u662f\u8f93\u5165\u683c\u5f0f\uff0c\u4ee5\u53ca\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362\uff0c\u8fd9\u4e9b\u662f\u5904\u7406\u6570\u503c\u8ba1\u7b97\u65f6\u5e38\u89c1\u7684\u6b65\u9aa4\u3002\u901a\u8fc7\u8fd9\u6837\u7684\u65b9\u6cd5\uff0c\u5373\u4f7f\u662f\u521d\u5b66\u8005\u4e5f\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5bf9\u4e00\u7ec4\u6570\u5b57\u7684\u57fa\u672c\u6570\u5b66\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q1: \u5982\u4f55\u5728Python\u4e2d\u8f93\u5165\u4e00\u7cfb\u5217\u6570\u5b57\uff1f<\/strong><\/p>\n<p>\u63d0\u4f9b\u4e00\u4e2a\u7528\u6237\u754c\u9762\u6216\u8005\u8ba9\u7528\u6237\u76f4\u63a5\u5728\u7ec8\u7aef\u8f93\u5165\u4e00\u7cfb\u5217\u6570\u5b57\u662f\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>input<\/code>\u51fd\u6570\u6765\u63a5\u6536\u7528\u6237\u7684\u8f93\u5165\uff0c\u5e76\u4fdd\u5b58\u4e3a\u4e00\u4e2a\u5217\u8868\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-python\">numbers = input(&quot;\u8bf7\u8f93\u5165\u4e00\u7cfb\u5217\u6570\u5b57\uff0c\u4ee5\u7a7a\u683c\u5206\u9694\uff1a&quot;).split()  # \u4ee5\u7a7a\u683c\u5206\u5272\u7528\u6237\u8f93\u5165\u7684\u6570\u5b57\nnumbers = [int(num) for num in numbers]  # \u5c06\u6570\u5b57\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\n<\/code><\/pre>\n<p><strong>Q2: \u5982\u4f55\u8ba1\u7b97\u4e00\u7cfb\u5217\u6570\u5b57\u7684\u548c\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528Python\u7684<code>sum<\/code>\u51fd\u6570\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u8ba1\u7b97\u51fa\u5217\u8868\u4e2d\u6240\u6709\u6570\u5b57\u7684\u548c\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u5df2\u7ecf\u6709\u4e00\u4e2a\u540d\u4e3a<code>numbers<\/code>\u7684\u5217\u8868\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u8ba1\u7b97\u5b83\u4eec\u7684\u548c\uff1a<\/p>\n<pre><code class=\"language-python\">sum_of_numbers = sum(numbers)\n<\/code><\/pre>\n<p><strong>Q3: \u5982\u4f55\u8ba1\u7b97\u4e00\u7cfb\u5217\u6570\u5b57\u7684\u5e73\u5747\u6570\uff1f<\/strong><\/p>\n<p>\u8981\u8ba1\u7b97\u4e00\u7cfb\u5217\u6570\u5b57\u7684\u5e73\u5747\u6570\uff0c\u53ef\u4ee5\u5148\u8ba1\u7b97\u6240\u6709\u6570\u5b57\u7684\u548c\uff0c\u7136\u540e\u9664\u4ee5\u6570\u5b57\u7684\u4e2a\u6570\u3002\u53ef\u4ee5\u4f7f\u7528<code>len<\/code>\u51fd\u6570\u83b7\u53d6\u5217\u8868\u4e2d\u7684\u5143\u7d20\u4e2a\u6570\uff0c\u5e76\u4f7f\u7528\u4e0a\u8ff0\u8ba1\u7b97\u548c\u7684\u65b9\u6cd5\u5f97\u5230\u6570\u5b57\u7684\u548c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">average = sum(numbers) \/ len(numbers)\n<\/code><\/pre>\n<p>\u8bf7\u6ce8\u610f\uff0c\u5982\u679c\u4f60\u7684\u4ee3\u7801\u6d89\u53ca\u5230\u9664\u6cd5\u8fd0\u7b97\uff0c\u4e3a\u4e86\u907f\u514d\u9664\u96f6\u9519\u8bef\uff0c\u4f60\u9700\u8981\u5728\u8ba1\u7b97\u5e73\u5747\u6570\u4e4b\u524d\u786e\u4fdd\u5217\u8868\u4e0d\u4e3a\u7a7a\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>if<\/code>\u8bed\u53e5\u6765\u68c0\u67e5\u5217\u8868\u662f\u5426\u4e3a\u7a7a\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-python\">if len(numbers) &gt; 0:\n    average = sum(numbers) \/ len(numbers)\nelse:\n    average = 0  # \u5982\u679c\u5217\u8868\u4e3a\u7a7a\uff0c\u5219\u5c06\u5e73\u5747\u503c\u8bbe\u7f6e\u4e3a0\u6216\u5176\u4ed6\u9ed8\u8ba4\u503c\n<\/code><\/pre>\n<p>\u5e0c\u671b\u8fd9\u4e9b\u56de\u7b54\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5982\u679c\u8fd8\u6709\u5176\u4ed6\u95ee\u9898\uff0c\u8bf7\u968f\u65f6\u63d0\u95ee\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8f93\u5165\u4e00\u7cfb\u5217\u6570\u5b57\u5e76\u8ba1\u7b97\u5b83\u4eec\u7684\u548c\u4e0e\u5e73\u5747\u6570\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u51fd\u6570input()\u6765\u63a5\u6536\u8f93\u5165\uff0c\u7136\u540e\u4f7f\u7528\u5faa [&hellip;]","protected":false},"author":3,"featured_media":177063,"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\/177058"}],"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=177058"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/177058\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/177063"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=177058"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=177058"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=177058"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}