{"id":1100907,"date":"2025-01-08T15:44:16","date_gmt":"2025-01-08T07:44:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1100907.html"},"modified":"2025-01-08T15:44:18","modified_gmt":"2025-01-08T07:44:18","slug":"%e5%a6%82%e4%bd%95%e8%be%93%e5%85%a5%e4%b8%80%e4%b8%aa%e6%ad%a3%e6%95%b4%e6%95%b0python-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1100907.html","title":{"rendered":"\u5982\u4f55\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570Python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063932\/80cb2d37-5227-49bf-84eb-483af25e3892.webp\" alt=\"\u5982\u4f55\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570Python\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528input()\u51fd\u6570\u3001\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u786e\u4fdd\u8f93\u5165\u4e3a\u6574\u6570\u3001\u4f7f\u7528while\u5faa\u73af\u53cd\u590d\u8bf7\u6c42\u8f93\u5165\uff0c\u76f4\u5230\u83b7\u5f97\u6b63\u786e\u7684\u6b63\u6574\u6570\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u7f16\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u7528\u6237\u8f93\u5165\u6570\u636e\uff0c\u5176\u4e2d\u5305\u62ec\u6b63\u6574\u6570\u3002\u4e3a\u4e86\u786e\u4fdd\u8f93\u5165\u7b26\u5408\u8981\u6c42\uff0c\u9700\u8981\u8003\u8651\u591a\u79cd\u60c5\u51b5\uff0c\u5982\u975e\u6574\u6570\u3001\u8d1f\u6570\u3001\u7a7a\u8f93\u5165\u7b49\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u51e0\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u6b63\u6574\u6570\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u57fa\u672c\u8f93\u5165\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86input()\u51fd\u6570\u6765\u83b7\u53d6\u7528\u6237\u8f93\u5165\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cinput()\u51fd\u6570\u4f1a\u5c06\u8f93\u5165\u7684\u6570\u636e\u4f5c\u4e3a\u5b57\u7b26\u4e32\u5904\u7406\uff0c\u6240\u4ee5\u9700\u8981\u4f7f\u7528int()\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = int(input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570: &quot;))<\/p>\n<p>print(&quot;\u4f60\u8f93\u5165\u7684\u6b63\u6574\u6570\u662f:&quot;, number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f46\u662f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u6ca1\u6709\u8003\u8651\u5f02\u5e38\u60c5\u51b5\uff0c\u5982\u8f93\u5165\u7684\u4e0d\u662f\u6574\u6570\u65f6\u4f1a\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u5904\u7406\u8f93\u5165\u9519\u8bef\uff0c\u53ef\u4ee5\u4f7f\u7528try-except\u8bed\u53e5\u6355\u83b7\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    number = int(input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570: &quot;))<\/p>\n<p>    if number &lt;= 0:<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;\u8f93\u5165\u7684\u4e0d\u662f\u6b63\u6574\u6570&quot;)<\/p>\n<p>    print(&quot;\u4f60\u8f93\u5165\u7684\u6b63\u6574\u6570\u662f:&quot;, number)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(&quot;\u8f93\u5165\u9519\u8bef:&quot;, e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u6709\u6548\u5730\u5904\u7406\u4e86\u8f93\u5165\u7684\u9519\u8bef\u7c7b\u578b\uff0c\u4f46\u8fd8\u662f\u53ea\u8fdb\u884c\u4e86\u4e00\u6b21\u8f93\u5165\u5c1d\u8bd5\u3002\u5982\u679c\u7528\u6237\u591a\u6b21\u8f93\u5165\u9519\u8bef\uff0c\u9700\u8981\u5faa\u73af\u6765\u91cd\u65b0\u8bf7\u6c42\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5faa\u73af\u53cd\u590d\u8bf7\u6c42\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u4e3a\u786e\u4fdd\u7528\u6237\u8f93\u5165\u6b63\u786e\u7684\u6b63\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528while\u5faa\u73af\u53cd\u590d\u8bf7\u6c42\u8f93\u5165\uff0c\u76f4\u5230\u8f93\u5165\u5408\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    try:<\/p>\n<p>        number = int(input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570: &quot;))<\/p>\n<p>        if number &gt; 0:<\/p>\n<p>            break<\/p>\n<p>        else:<\/p>\n<p>            print(&quot;\u8f93\u5165\u7684\u4e0d\u662f\u6b63\u6574\u6570\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\u3002&quot;)<\/p>\n<p>    except ValueError:<\/p>\n<p>        print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570\u3002&quot;)<\/p>\n<p>print(&quot;\u4f60\u8f93\u5165\u7684\u6b63\u6574\u6570\u662f:&quot;, number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7efc\u5408\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p>\u7efc\u5408\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u7a0b\u5e8f\u793a\u4f8b\uff0c\u786e\u4fdd\u7528\u6237\u8f93\u5165\u6b63\u786e\u7684\u6b63\u6574\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_positive_integer():<\/p>\n<p>    while True:<\/p>\n<p>        try:<\/p>\n<p>            user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570: &quot;)<\/p>\n<p>            number = int(user_input)<\/p>\n<p>            if number &lt;= 0:<\/p>\n<p>                print(&quot;\u8f93\u5165\u7684\u4e0d\u662f\u6b63\u6574\u6570\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\u3002&quot;)<\/p>\n<p>            else:<\/p>\n<p>                return number<\/p>\n<p>        except ValueError:<\/p>\n<p>            print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570\u3002&quot;)<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u83b7\u53d6\u6b63\u6574\u6570<\/strong><\/h2>\n<p>positive_integer = get_positive_integer()<\/p>\n<p>print(&quot;\u4f60\u8f93\u5165\u7684\u6b63\u6574\u6570\u662f:&quot;, positive_integer)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u8fdb\u4e00\u6b65\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\u4ee3\u7801\uff0c\u589e\u52a0\u8f93\u5165\u63d0\u793a\u4fe1\u606f\u548c\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u591a\u6b21\u8f93\u5165\u9519\u8bef\u540e\u7ed9\u51fa\u66f4\u8be6\u7ec6\u7684\u63d0\u793a\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_positive_integer():<\/p>\n<p>    attempts = 0<\/p>\n<p>    while True:<\/p>\n<p>        try:<\/p>\n<p>            user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570: &quot;)<\/p>\n<p>            number = int(user_input)<\/p>\n<p>            if number &lt;= 0:<\/p>\n<p>                print(&quot;\u8f93\u5165\u7684\u4e0d\u662f\u6b63\u6574\u6570\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\u3002&quot;)<\/p>\n<p>            else:<\/p>\n<p>                return number<\/p>\n<p>        except ValueError:<\/p>\n<p>            print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570\u3002&quot;)<\/p>\n<p>        attempts += 1<\/p>\n<p>        if attempts &gt;= 3:<\/p>\n<p>            print(&quot;\u591a\u6b21\u8f93\u5165\u9519\u8bef\uff0c\u8bf7\u4ed4\u7ec6\u9605\u8bfb\u63d0\u793a\u4fe1\u606f\u3002&quot;)<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u83b7\u53d6\u6b63\u6574\u6570<\/strong><\/h2>\n<p>positive_integer = get_positive_integer()<\/p>\n<p>print(&quot;\u4f60\u8f93\u5165\u7684\u6b63\u6574\u6570\u662f:&quot;, positive_integer)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5b9e\u73b0\u8f93\u5165\u6b63\u6574\u6570\u7684\u5173\u952e\u6b65\u9aa4\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528input()\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\u3002<\/li>\n<li>\u4f7f\u7528int()\u51fd\u6570\u5c06\u8f93\u5165\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5e76\u68c0\u67e5\u5176\u662f\u5426\u4e3a\u6b63\u6574\u6570\u3002<\/li>\n<li>\u4f7f\u7528try-except\u8bed\u53e5\u5904\u7406\u8f93\u5165\u9519\u8bef\u3002<\/li>\n<li>\u4f7f\u7528while\u5faa\u73af\u53cd\u590d\u8bf7\u6c42\u8f93\u5165\uff0c\u76f4\u5230\u8f93\u5165\u6b63\u786e\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u6b63\u6574\u6570\u5408\u6cd5\u4e14\u7b26\u5408\u8981\u6c42\uff0c\u63d0\u9ad8\u4e86\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u662f\u6b63\u6574\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5\u6765\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u662f\u6b63\u6574\u6570\u3002\u9996\u5148\uff0c\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u83b7\u53d6\u7528\u6237\u7684\u8f93\u5165\uff0c\u7136\u540e\u901a\u8fc7<code>isdigit()<\/code>\u65b9\u6cd5\u68c0\u67e5\u8f93\u5165\u662f\u5426\u4e3a\u6570\u5b57\u3002\u5982\u679c\u662f\u6570\u5b57\uff0c\u518d\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u5e76\u68c0\u67e5\u662f\u5426\u5927\u4e8e\u96f6\u3002\u5982\u679c\u4e0d\u662f\uff0c\u53ef\u4ee5\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u6709\u6548\u907f\u514d\u65e0\u6548\u8f93\u5165\u3002<\/p>\n<p><strong>Python\u4e2d\u8f93\u5165\u6b63\u6574\u6570\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u6700\u4f73\u5b9e\u8df5\u662f\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6765\u6355\u83b7\u7528\u6237\u8f93\u5165\u7684\u9519\u8bef\u3002\u5229\u7528<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u53ef\u4ee5\u5904\u7406\u4e0d\u5408\u6cd5\u8f93\u5165\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u5c06\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5e76\u68c0\u67e5\u8be5\u503c\u662f\u5426\u5927\u4e8e\u96f6\u3002\u5982\u679c\u8f93\u5165\u4e0d\u5408\u6cd5\uff0c\u7cfb\u7edf\u5c06\u629b\u51fa\u5f02\u5e38\uff0c\u7528\u6237\u53ef\u4ee5\u5f97\u5230\u53cb\u597d\u7684\u63d0\u793a\uff0c\u5e2e\u52a9\u4ed6\u4eec\u91cd\u65b0\u8f93\u5165\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u4f7f\u7528\u51fd\u6570\u6765\u8f93\u5165\u6b63\u6574\u6570\uff1f<\/strong><br \/>\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u4e13\u95e8\u7684\u51fd\u6570\u6765\u5904\u7406\u6b63\u6574\u6570\u7684\u8f93\u5165\u3002\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u5305\u542b\u5faa\u73af\u4ee5\u53cd\u590d\u8bf7\u6c42\u7528\u6237\u8f93\u5165\uff0c\u76f4\u5230\u83b7\u5f97\u6709\u6548\u7684\u6b63\u6574\u6570\u4e3a\u6b62\u3002\u901a\u8fc7\u5c06\u8f93\u5165\u903b\u8f91\u5c01\u88c5\u5728\u51fd\u6570\u4e2d\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u4e0e\u53ef\u8bfb\u6027\uff0c\u8ba9\u7a0b\u5e8f\u66f4\u52a0\u6a21\u5757\u5316\u3002\u8fd9\u6837\uff0c\u8c03\u7528\u8be5\u51fd\u6570\u65f6\u5c31\u53ef\u4ee5\u76f4\u63a5\u5f97\u5230\u4e00\u4e2a\u6709\u6548\u7684\u6b63\u6574\u6570\uff0c\u65b9\u4fbf\u540e\u7eed\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528input()\u51fd\u6570\u3001\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u786e\u4fdd\u8f93\u5165\u4e3a\u6574\u6570\u3001\u4f7f\u7528while\u5faa\u73af\u53cd\u590d [&hellip;]","protected":false},"author":3,"featured_media":1100914,"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\/1100907"}],"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=1100907"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100907\/revisions"}],"predecessor-version":[{"id":1100915,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100907\/revisions\/1100915"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1100914"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1100907"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1100907"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1100907"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}