{"id":1045513,"date":"2024-12-31T13:23:49","date_gmt":"2024-12-31T05:23:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1045513.html"},"modified":"2024-12-31T13:23:54","modified_gmt":"2024-12-31T05:23:54","slug":"%e7%94%a8python%e5%a6%82%e4%bd%95%e6%94%b9%e5%86%99%e7%a8%8b%e5%ba%8f%e7%9a%84%e8%be%93%e5%85%a5%e6%ac%a1%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1045513.html","title":{"rendered":"\u7528python\u5982\u4f55\u6539\u5199\u7a0b\u5e8f\u7684\u8f93\u5165\u6b21\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/7829ebb1-8ffd-414e-825c-5e7b238234fe.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u7528python\u5982\u4f55\u6539\u5199\u7a0b\u5e8f\u7684\u8f93\u5165\u6b21\u6570\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u6539\u5199\u7a0b\u5e8f\u7684\u8f93\u5165\u6b21\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4f8b\u5982\u4f7f\u7528\u5faa\u73af\u3001\u51fd\u6570\u6216\u7c7b\u6765\u63a7\u5236\u8f93\u5165\u6b21\u6570\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\uff1a\u5faa\u73af\u63a7\u5236\u3001\u51fd\u6570\u53c2\u6570\u63a7\u5236\u3001\u7c7b\u7684\u5c5e\u6027\u63a7\u5236\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u8fdb\u884c\u6f14\u793a\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5faa\u73af\u63a7\u5236\u8f93\u5165\u6b21\u6570<\/h3>\n<\/p>\n<p><p>\u5faa\u73af\u63a7\u5236\u662f\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u4e4b\u4e00\uff0c\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u6216<code>while<\/code>\u5faa\u73af\u6765\u63a7\u5236\u7a0b\u5e8f\u7684\u8f93\u5165\u6b21\u6570\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u548c<code>while<\/code>\u5faa\u73af\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528for\u5faa\u73af\u63a7\u5236\u8f93\u5165\u6b21\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def input_n_times(n):<\/p>\n<p>    inputs = []<\/p>\n<p>    for _ in range(n):<\/p>\n<p>        user_input = input(&quot;Enter something: &quot;)<\/p>\n<p>        inputs.append(user_input)<\/p>\n<p>    return inputs<\/p>\n<p>n = int(input(&quot;How many times do you want to input? &quot;))<\/p>\n<p>result = input_n_times(n)<\/p>\n<p>print(&quot;You entered:&quot;, result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>input_n_times<\/code>\uff0c\u5b83\u63a5\u6536\u4e00\u4e2a\u53c2\u6570<code>n<\/code>\uff0c\u8868\u793a\u8f93\u5165\u7684\u6b21\u6570\u3002\u7136\u540e\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u6765\u63a7\u5236\u8f93\u5165\u6b21\u6570\uff0c\u6bcf\u6b21\u8f93\u5165\u7684\u5185\u5bb9\u5b58\u50a8\u5728\u5217\u8868<code>inputs<\/code>\u4e2d\uff0c\u6700\u540e\u8fd4\u56de\u6240\u6709\u8f93\u5165\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528while\u5faa\u73af\u63a7\u5236\u8f93\u5165\u6b21\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def input_n_times(n):<\/p>\n<p>    inputs = []<\/p>\n<p>    count = 0<\/p>\n<p>    while count &lt; n:<\/p>\n<p>        user_input = input(&quot;Enter something: &quot;)<\/p>\n<p>        inputs.append(user_input)<\/p>\n<p>        count += 1<\/p>\n<p>    return inputs<\/p>\n<p>n = int(input(&quot;How many times do you want to input? &quot;))<\/p>\n<p>result = input_n_times(n)<\/p>\n<p>print(&quot;You entered:&quot;, result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u6765\u63a7\u5236\u8f93\u5165\u6b21\u6570\uff0c\u901a\u8fc7\u4e00\u4e2a\u8ba1\u6570\u5668<code>count<\/code>\u6765\u8bb0\u5f55\u5df2\u7ecf\u8f93\u5165\u7684\u6b21\u6570\uff0c\u5f53<code>count<\/code>\u5c0f\u4e8e<code>n<\/code>\u65f6\uff0c\u4e0d\u65ad\u8fdb\u884c\u8f93\u5165\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u51fd\u6570\u53c2\u6570\u63a7\u5236\u8f93\u5165\u6b21\u6570<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u51fd\u6570\u53c2\u6570\u63a7\u5236\u8f93\u5165\u6b21\u6570\uff0c\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u4e8e\u7ef4\u62a4\u3002\u4ee5\u4e0b\u662f\u901a\u8fc7\u51fd\u6570\u53c2\u6570\u63a7\u5236\u8f93\u5165\u6b21\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def input_n_times(n):<\/p>\n<p>    return [input(&quot;Enter something: &quot;) for _ in range(n)]<\/p>\n<p>n = int(input(&quot;How many times do you want to input? &quot;))<\/p>\n<p>result = input_n_times(n)<\/p>\n<p>print(&quot;You entered:&quot;, result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\uff08list comprehension\uff09\u6765\u7b80\u5316\u8f93\u5165\u64cd\u4f5c\uff0c\u901a\u8fc7<code>for<\/code>\u5faa\u73af\u904d\u5386<code>n<\/code>\u6b21\uff0c\u6bcf\u6b21\u8f93\u5165\u7684\u5185\u5bb9\u76f4\u63a5\u5b58\u50a8\u5728\u5217\u8868\u4e2d\uff0c\u6700\u540e\u8fd4\u56de\u6240\u6709\u8f93\u5165\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7c7b\u7684\u5c5e\u6027\u63a7\u5236\u8f93\u5165\u6b21\u6570<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u7c7b\u7684\u5c5e\u6027\u63a7\u5236\u8f93\u5165\u6b21\u6570\uff0c\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u5177\u7ed3\u6784\u5316\u548c\u53ef\u6269\u5c55\u6027\u3002\u4ee5\u4e0b\u662f\u901a\u8fc7\u7c7b\u7684\u5c5e\u6027\u63a7\u5236\u8f93\u5165\u6b21\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class InputController:<\/p>\n<p>    def __init__(self, n):<\/p>\n<p>        self.n = n<\/p>\n<p>        self.inputs = []<\/p>\n<p>    def collect_inputs(self):<\/p>\n<p>        for _ in range(self.n):<\/p>\n<p>            user_input = input(&quot;Enter something: &quot;)<\/p>\n<p>            self.inputs.append(user_input)<\/p>\n<p>    def get_inputs(self):<\/p>\n<p>        return self.inputs<\/p>\n<p>n = int(input(&quot;How many times do you want to input? &quot;))<\/p>\n<p>controller = InputController(n)<\/p>\n<p>controller.collect_inputs()<\/p>\n<p>print(&quot;You entered:&quot;, controller.get_inputs())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7c7b<code>InputController<\/code>\uff0c\u5b83\u63a5\u6536\u4e00\u4e2a\u53c2\u6570<code>n<\/code>\uff0c\u8868\u793a\u8f93\u5165\u7684\u6b21\u6570\u3002\u901a\u8fc7\u65b9\u6cd5<code>collect_inputs<\/code>\u6765\u63a7\u5236\u8f93\u5165\u64cd\u4f5c\uff0c\u6240\u6709\u8f93\u5165\u5185\u5bb9\u5b58\u50a8\u5728\u5b9e\u4f8b\u5c5e\u6027<code>inputs<\/code>\u4e2d\uff0c\u6700\u540e\u901a\u8fc7\u65b9\u6cd5<code>get_inputs<\/code>\u8fd4\u56de\u6240\u6709\u8f93\u5165\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236Python\u7a0b\u5e8f\u7684\u8f93\u5165\u6b21\u6570\u3002<strong>\u5faa\u73af\u63a7\u5236\u3001\u51fd\u6570\u53c2\u6570\u63a7\u5236\u548c\u7c7b\u7684\u5c5e\u6027\u63a7\u5236<\/strong>\u90fd\u662f\u5e38\u89c1\u4e14\u6709\u6548\u7684\u65b9\u5f0f\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p><strong>\u5faa\u73af\u63a7\u5236<\/strong>\u9002\u7528\u4e8e\u7b80\u5355\u7684\u8f93\u5165\u6b21\u6570\u63a7\u5236\uff0c<strong>\u51fd\u6570\u53c2\u6570\u63a7\u5236<\/strong>\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c<strong>\u7c7b\u7684\u5c5e\u6027\u63a7\u5236<\/strong>\u5219\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u573a\u666f\uff0c\u5177\u6709\u826f\u597d\u7684\u6269\u5c55\u6027\u548c\u7ed3\u6784\u6027\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u6765\u63a7\u5236\u7a0b\u5e8f\u7684\u8f93\u5165\u6b21\u6570\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5b9e\u73b0\u7a0b\u5e8f\u7684\u8f93\u5165\u6b21\u6570\u9650\u5236\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5\u6765\u63a7\u5236\u7a0b\u5e8f\u7684\u8f93\u5165\u6b21\u6570\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u6307\u5b9a\u8f93\u5165\u6b21\u6570\uff0c\u5e76\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\u8bf7\u6c42\u7528\u6237\u8f93\u5165\u3002\u8fd9\u6837\uff0c\u7a0b\u5e8f\u80fd\u591f\u6839\u636e\u8bbe\u5b9a\u7684\u6b21\u6570\u8fdb\u884c\u8f93\u5165\u3002<\/p>\n<p><strong>\u600e\u6837\u5728Python\u4e2d\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5904\u7406\u7528\u6237\u8f93\u5165\u9519\u8bef\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u6765\u5b9e\u73b0\u3002\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u53ef\u4ee5\u88ab\u9a8c\u8bc1\uff0c\u4ee5\u786e\u4fdd\u5b83\u7b26\u5408\u9884\u671f\u7684\u683c\u5f0f\u6216\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6355\u83b7<code>ValueError<\/code>\uff0c\u4ee5\u9632\u7528\u6237\u8f93\u5165\u4e86\u65e0\u6cd5\u8f6c\u6362\u4e3a\u6570\u5b57\u7684\u5185\u5bb9\u3002<\/p>\n<p><strong>\u5982\u4f55\u6839\u636e\u9700\u8981\u52a8\u6001\u8c03\u6574\u8f93\u5165\u6b21\u6570\uff1f<\/strong><br \/>\u82e5\u5e0c\u671b\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u8c03\u6574\u8f93\u5165\u6b21\u6570\uff0c\u53ef\u4ee5\u5148\u83b7\u53d6\u7528\u6237\u7684\u8f93\u5165\u6b21\u6570\uff0c\u7136\u540e\u4f7f\u7528\u8fd9\u4e2a\u6570\u503c\u6765\u63a7\u5236\u8f93\u5165\u5faa\u73af\u7684\u6b21\u6570\u3002\u53ef\u4ee5\u901a\u8fc7\u8f93\u5165\u63d0\u793a\u8ba9\u7528\u6237\u8f93\u5165\u60f3\u8981\u7684\u6b21\u6570\uff0c\u7a0b\u5e8f\u968f\u540e\u4f1a\u6839\u636e\u8fd9\u4e2a\u6b21\u6570\u8fdb\u884c\u76f8\u5e94\u7684\u8f93\u5165\u8bf7\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u6539\u5199\u7a0b\u5e8f\u7684\u8f93\u5165\u6b21\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4f8b\u5982\u4f7f\u7528\u5faa\u73af\u3001\u51fd\u6570\u6216\u7c7b\u6765\u63a7\u5236\u8f93\u5165\u6b21\u6570\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\uff1a [&hellip;]","protected":false},"author":3,"featured_media":1045530,"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\/1045513"}],"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=1045513"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1045513\/revisions"}],"predecessor-version":[{"id":1045531,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1045513\/revisions\/1045531"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1045530"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1045513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1045513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1045513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}