{"id":1133443,"date":"2025-01-08T21:06:32","date_gmt":"2025-01-08T13:06:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1133443.html"},"modified":"2025-01-08T21:06:35","modified_gmt":"2025-01-08T13:06:35","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba%e8%be%93%e5%85%a5%e4%b8%80%e4%b8%aa%e6%ad%a3%e6%95%b4%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1133443.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8868\u793a\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102701\/b768f0fb-52d9-4922-8278-f6d5c7abbf57.webp\" alt=\"python\u4e2d\u5982\u4f55\u8868\u793a\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u8868\u793a\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570\u7684\u65b9\u6cd5\u6709\u51e0\u79cd\uff1a\u4f7f\u7528input\u51fd\u6570\u3001\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3001\u8fdb\u884c\u5f02\u5e38\u5904\u7406\u6765\u786e\u4fdd\u8f93\u5165\u662f\u6b63\u6574\u6570\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u7ed3\u5408<code>input()<\/code>\u51fd\u6570\u548c<code>int()<\/code>\u51fd\u6570\uff0c\u5e76\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u548c\u5f02\u5e38\u5904\u7406\u6765\u786e\u4fdd\u8f93\u5165\u7684\u503c\u662f\u6b63\u6574\u6570\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u4e14\u63d0\u4f9b\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528input\u51fd\u6570\u8fdb\u884c\u8f93\u5165<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>input()<\/code>\u51fd\u6570\u7528\u4e8e\u4ece\u7528\u6237\u8f93\u5165\u4e2d\u8bfb\u53d6\u5b57\u7b26\u4e32\u3002\u4e3a\u4e86\u786e\u4fdd\u8f93\u5165\u7684\u662f\u4e00\u4e2a\u6b63\u6574\u6570\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u8f93\u5165\u7684\u5185\u5bb9\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u548c\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<p><h3><strong>1\u3001\u57fa\u672c\u8f93\u5165\u4e0e\u7c7b\u578b\u8f6c\u6362<\/strong><\/h3>\n<\/p>\n<p><p>\u5728\u6700\u57fa\u672c\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u8bfb\u53d6\u7528\u6237\u8f93\u5165\uff0c\u7136\u540e\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570: &quot;)<\/p>\n<p>try:<\/p>\n<p>    number = int(user_input)<\/p>\n<p>    if number &gt; 0:<\/p>\n<p>        print(f&quot;\u60a8\u8f93\u5165\u7684\u6b63\u6574\u6570\u662f: {number}&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570\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><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>try-except<\/code>\u5757\u6765\u6355\u83b7\u4efb\u4f55\u7c7b\u578b\u8f6c\u6362\u9519\u8bef\uff08\u5373\u7528\u6237\u8f93\u5165\u7684\u4e0d\u662f\u4e00\u4e2a\u6709\u6548\u7684\u6574\u6570\uff09\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5<code>if<\/code>\u6765\u786e\u4fdd\u8f93\u5165\u7684\u662f\u4e00\u4e2a\u6b63\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3><strong>2\u3001\u4f7f\u7528while\u5faa\u73af\u8fdb\u884c\u8f93\u5165\u9a8c\u8bc1<\/strong><\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u662f\u4e00\u4e2a\u6b63\u6574\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u6765\u91cd\u590d\u63d0\u793a\u7528\u6237\u8f93\u5165\uff0c\u76f4\u5230\u4ed6\u4eec\u8f93\u5165\u6709\u6548\u7684\u6b63\u6574\u6570\u4e3a\u6b62\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570: &quot;)<\/p>\n<p>    try:<\/p>\n<p>        number = int(user_input)<\/p>\n<p>        if number &gt; 0:<\/p>\n<p>            print(f&quot;\u60a8\u8f93\u5165\u7684\u6b63\u6574\u6570\u662f: {number}&quot;)<\/p>\n<p>            break<\/p>\n<p>        else:<\/p>\n<p>            print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570\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><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>while True<\/code>\u5faa\u73af\u4e0d\u65ad\u63d0\u793a\u7528\u6237\u8f93\u5165\uff0c\u76f4\u5230\u4ed6\u4eec\u8f93\u5165\u4e00\u4e2a\u6709\u6548\u7684\u6b63\u6574\u6570\u3002<code>break<\/code>\u8bed\u53e5\u7528\u4e8e\u9000\u51fa\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5f02\u5e38\u5904\u7406<\/p>\n<\/p>\n<p><p>\u5728\u7f16\u7a0b\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u662f\u786e\u4fdd\u7a0b\u5e8f\u5065\u58ee\u6027\u7684\u91cd\u8981\u90e8\u5206\u3002\u901a\u8fc7\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\uff0c\u6211\u4eec\u53ef\u4ee5\u907f\u514d\u7a0b\u5e8f\u56e0\u7528\u6237\u8f93\u5165\u65e0\u6548\u6570\u636e\u800c\u5d29\u6e83\u3002\u5728\u8f93\u5165\u6b63\u6574\u6570\u7684\u573a\u666f\u4e2d\uff0c\u6700\u5e38\u89c1\u7684\u5f02\u5e38\u662f<code>ValueError<\/code>\uff0c\u8fd9\u662f\u7531\u4e8e<code>int()<\/code>\u51fd\u6570\u65e0\u6cd5\u5c06\u975e\u6570\u5b57\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u65f6\u5f15\u53d1\u7684\u3002<\/p>\n<\/p>\n<p><h3><strong>1\u3001\u6355\u83b7ValueError<\/strong><\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u662f\u4e00\u4e2a\u6709\u6548\u7684\u6b63\u6574\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u5757\u6355\u83b7<code>ValueError<\/code>\u5f02\u5e38\uff0c\u5e76\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_positive_integer():<\/p>\n<p>    while True:<\/p>\n<p>        user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570: &quot;)<\/p>\n<p>        try:<\/p>\n<p>            number = int(user_input)<\/p>\n<p>            if number &gt; 0:<\/p>\n<p>                return number<\/p>\n<p>            else:<\/p>\n<p>                print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570\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>positive_number = get_positive_integer()<\/p>\n<p>print(f&quot;\u60a8\u8f93\u5165\u7684\u6b63\u6574\u6570\u662f: {positive_number}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>get_positive_integer()<\/code>\uff0c\u8be5\u51fd\u6570\u4f7f\u7528<code>while True<\/code>\u5faa\u73af\u63d0\u793a\u7528\u6237\u8f93\u5165\uff0c\u5e76\u901a\u8fc7<code>try-except<\/code>\u5757\u6355\u83b7<code>ValueError<\/code>\u5f02\u5e38\u3002\u5982\u679c\u8f93\u5165\u6709\u6548\u4e14\u4e3a\u6b63\u6574\u6570\uff0c\u5219\u8fd4\u56de\u8be5\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3><strong>2\u3001\u6355\u83b7\u591a\u79cd\u5f02\u5e38<\/strong><\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u6355\u83b7\u591a\u79cd\u5f02\u5e38\u3002\u867d\u7136\u5728\u8f93\u5165\u6b63\u6574\u6570\u7684\u573a\u666f\u4e2d\uff0c<code>ValueError<\/code>\u662f\u6700\u5e38\u89c1\u7684\u5f02\u5e38\uff0c\u4f46\u6211\u4eec\u4e5f\u53ef\u4ee5\u6355\u83b7\u5176\u4ed6\u5f02\u5e38\uff0c\u4f8b\u5982<code>KeyboardInterrupt<\/code>\uff0c\u4ee5\u4fbf\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u6309<code>Ctrl+C<\/code>\u9000\u51fa\u8f93\u5165\u5faa\u73af\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\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 &gt; 0:<\/p>\n<p>                return number<\/p>\n<p>            else:<\/p>\n<p>                print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570\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>        except KeyboardInterrupt:<\/p>\n<p>            print(&quot;\\n\u8f93\u5165\u5df2\u53d6\u6d88\u3002&quot;)<\/p>\n<p>            return None<\/p>\n<p>positive_number = get_positive_integer()<\/p>\n<p>if positive_number:<\/p>\n<p>    print(f&quot;\u60a8\u8f93\u5165\u7684\u6b63\u6574\u6570\u662f: {positive_number}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u672a\u8f93\u5165\u6b63\u6574\u6570\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u6355\u83b7<code>KeyboardInterrupt<\/code>\u5f02\u5e38\u6765\u5904\u7406\u7528\u6237\u53d6\u6d88\u8f93\u5165\u7684\u60c5\u51b5\uff0c\u5e76\u5728\u6355\u83b7\u5230\u8be5\u5f02\u5e38\u65f6\u8fd4\u56de<code>None<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u9a8c\u8bc1<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u5904\u7406\u5b57\u7b26\u4e32\u548c\u9a8c\u8bc1\u8f93\u5165\u7684\u4e00\u79cd\u5f3a\u5927\u5de5\u5177\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>re<\/code>\u6a21\u5757\u6765\u7f16\u5199\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u4ee5\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u662f\u4e00\u4e2a\u6b63\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3><strong>1\u3001\u57fa\u672c\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/h3>\n<\/p>\n<p><p>\u5728\u57fa\u672c\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u662f\u5426\u4e3a\u4e00\u4e2a\u6b63\u6574\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def get_positive_integer():<\/p>\n<p>    while True:<\/p>\n<p>        user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570: &quot;)<\/p>\n<p>        if re.match(r&#39;^[1-9]\\d*$&#39;, user_input):<\/p>\n<p>            return int(user_input)<\/p>\n<p>        else:<\/p>\n<p>            print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570\u3002&quot;)<\/p>\n<p>positive_number = get_positive_integer()<\/p>\n<p>print(f&quot;\u60a8\u8f93\u5165\u7684\u6b63\u6574\u6570\u662f: {positive_number}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>re.match()<\/code>\u51fd\u6570\u548c\u6b63\u5219\u8868\u8fbe\u5f0f<code>^[1-9]\\d*$<\/code>\u6765\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u662f\u5426\u4e3a\u6b63\u6574\u6570\u3002\u8be5\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u4ee51-9\u5f00\u5934\u7684\u5b57\u7b26\u4e32\uff0c\u540e\u9762\u53ef\u4ee5\u8ddf\u968f\u4efb\u610f\u6570\u91cf\u7684\u6570\u5b57\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3><strong>2\u3001\u7ed3\u5408\u5f02\u5e38\u5904\u7406<\/strong><\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u9a8c\u8bc1\u4e0e\u5f02\u5e38\u5904\u7406\u7ed3\u5408\u8d77\u6765\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>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>            if re.match(r&#39;^[1-9]\\d*$&#39;, user_input):<\/p>\n<p>                return int(user_input)<\/p>\n<p>            else:<\/p>\n<p>                print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570\u3002&quot;)<\/p>\n<p>        except KeyboardInterrupt:<\/p>\n<p>            print(&quot;\\n\u8f93\u5165\u5df2\u53d6\u6d88\u3002&quot;)<\/p>\n<p>            return None<\/p>\n<p>positive_number = get_positive_integer()<\/p>\n<p>if positive_number:<\/p>\n<p>    print(f&quot;\u60a8\u8f93\u5165\u7684\u6b63\u6574\u6570\u662f: {positive_number}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u672a\u8f93\u5165\u6b63\u6574\u6570\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u7ed3\u5408\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u9a8c\u8bc1\u548c\u5f02\u5e38\u5904\u7406\uff0c\u4ee5\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u6709\u6548\u6027\u548c\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8868\u793a\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u8fdb\u884c\u8f93\u5165\u3001\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3001\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u3001\u4ee5\u53ca\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u9a8c\u8bc1\u3002\u4e3a\u4e86\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u6709\u6548\u6027\u548c\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u548c<code>int()<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u7528\u6237\u8f93\u5165\u4e2d\u8bfb\u53d6\u5b57\u7b26\u4e32\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u7ed3\u5408\u6761\u4ef6\u8bed\u53e5\u548c\u5f02\u5e38\u5904\u7406\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u8f93\u5165\u7684\u503c\u662f\u4e00\u4e2a\u6b63\u6574\u6570\u3002\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u53ef\u4ee5\u91cd\u590d\u63d0\u793a\u7528\u6237\u8f93\u5165\uff0c\u76f4\u5230\u4ed6\u4eec\u8f93\u5165\u6709\u6548\u7684\u6b63\u6574\u6570\u3002\u901a\u8fc7\u6355\u83b7\u548c\u5904\u7406<code>ValueError<\/code>\u5f02\u5e38\uff0c\u6211\u4eec\u53ef\u4ee5\u907f\u514d\u7a0b\u5e8f\u56e0\u65e0\u6548\u8f93\u5165\u800c\u5d29\u6e83\u3002\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u52a0\u7cbe\u786e\u5730\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0c\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570\u662fPython\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e2a\u5e38\u89c1\u9700\u6c42\uff0c\u901a\u8fc7\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u6709\u6548\u6027\uff0c\u4ece\u800c\u7f16\u5199\u51fa\u66f4\u52a0\u5065\u58ee\u548c\u53ef\u9760\u7684\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u6b63\u6574\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u63a5\u6536\u7528\u6237\u8f93\u5165\uff0c\u5e76\u7ed3\u5408\u6761\u4ef6\u8bed\u53e5\u8fdb\u884c\u9a8c\u8bc1\u3002\u53ef\u4ee5\u901a\u8fc7<code>isdigit()<\/code>\u65b9\u6cd5\u68c0\u67e5\u8f93\u5165\u662f\u5426\u4e3a\u6570\u5b57\uff0c\u5e76\u4e14\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u540e\u68c0\u67e5\u5176\u662f\u5426\u5927\u4e8e\u96f6\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570\uff1a&quot;)\nif user_input.isdigit() and int(user_input) &gt; 0:\n    print(&quot;\u60a8\u8f93\u5165\u7684\u6b63\u6574\u6570\u662f\uff1a&quot;, user_input)\nelse:\n    print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570\u3002&quot;)\n<\/code><\/pre>\n<p><strong>\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u4e0d\u662f\u6b63\u6574\u6570\uff0c\u6211\u8be5\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u4e3a\u4e86\u5904\u7406\u65e0\u6548\u8f93\u5165\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u53cd\u590d\u63d0\u793a\u7528\u6237\u8f93\u5165\uff0c\u76f4\u5230\u8f93\u5165\u6709\u6548\u4e3a\u6b62\u3002\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\uff0c\u786e\u4fdd\u7a0b\u5e8f\u4e0d\u4f1a\u56e0\u9519\u8bef\u8f93\u5165\u800c\u5d29\u6e83\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">while True:\n    user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570\uff1a&quot;)\n    try:\n        number = int(user_input)\n        if number &gt; 0:\n            print(&quot;\u60a8\u8f93\u5165\u7684\u6b63\u6574\u6570\u662f\uff1a&quot;, number)\n            break\n        else:\n            print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570\u3002&quot;)\n    except ValueError:\n        print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570\u3002&quot;)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5c06\u6b63\u6574\u6570\u7684\u8f93\u5165\u9650\u5236\u5728\u7279\u5b9a\u8303\u56f4\u5185\uff1f<\/strong><br \/>\u82e5\u9700\u8981\u9650\u5236\u8f93\u5165\u7684\u6b63\u6574\u6570\u5728\u7279\u5b9a\u8303\u56f4\u5185\uff0c\u53ef\u4ee5\u5728\u8f93\u5165\u9a8c\u8bc1\u65f6\u52a0\u5165\u989d\u5916\u7684\u6761\u4ef6\u3002\u4f8b\u5982\uff0c\u82e5\u8981\u786e\u4fdd\u8f93\u5165\u57281\u5230100\u4e4b\u95f4\uff0c\u53ef\u4ee5\u5982\u4e0b\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-python\">while True:\n    user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a1\u5230100\u4e4b\u95f4\u7684\u6b63\u6574\u6570\uff1a&quot;)\n    try:\n        number = int(user_input)\n        if 1 &lt;= number &lt;= 100:\n            print(&quot;\u60a8\u8f93\u5165\u7684\u6b63\u6574\u6570\u662f\uff1a&quot;, number)\n            break\n        else:\n            print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u786e\u4fdd\u8f93\u5165\u7684\u6570\u5b57\u57281\u5230100\u4e4b\u95f4\u3002&quot;)\n    except ValueError:\n        print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570\u3002&quot;)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u8868\u793a\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570\u7684\u65b9\u6cd5\u6709\u51e0\u79cd\uff1a\u4f7f\u7528input\u51fd\u6570\u3001\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3001\u8fdb\u884c\u5f02\u5e38\u5904\u7406\u6765\u786e\u4fdd\u8f93\u5165\u662f\u6b63\u6574\u6570 [&hellip;]","protected":false},"author":3,"featured_media":1133454,"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\/1133443"}],"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=1133443"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133443\/revisions"}],"predecessor-version":[{"id":1133456,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133443\/revisions\/1133456"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1133454"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1133443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1133443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1133443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}