{"id":1164868,"date":"2025-01-15T15:13:21","date_gmt":"2025-01-15T07:13:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1164868.html"},"modified":"2025-01-15T15:13:23","modified_gmt":"2025-01-15T07:13:23","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e8%a1%a8%e7%a4%ba%e6%ad%a3%e6%95%b4%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1164868.html","title":{"rendered":"\u5982\u4f55\u7528python\u8868\u793a\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\/25205340\/b08c6529-8fa5-4e08-94f9-48b465ab2321.webp\" alt=\"\u5982\u4f55\u7528python\u8868\u793a\u6b63\u6574\u6570\" \/><\/p>\n<p><p> <strong>\u7528Python\u8868\u793a\u6b63\u6574\u6570\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u6574\u6570\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u6216\u901a\u8fc7\u8f93\u5165\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\u3002<\/strong>\u5176\u4e2d\uff0c\u76f4\u63a5\u8d4b\u503c\u6574\u6570\u662f\u6700\u5e38\u89c1\u4e14\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u3002\u5728Python\u4e2d\uff0c\u6574\u6570\u5b57\u9762\u91cf\u76f4\u63a5\u8868\u793a\u6b63\u6574\u6570\u3002\u4f8b\u5982\uff0c\u8d4b\u503c\u8bed\u53e5 <code>a = 5<\/code> \u5373\u8868\u793a <code>a<\/code> \u662f\u4e00\u4e2a\u6b63\u6574\u6570\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e09\u79cd\u65b9\u5f0f\u4e2d\u7684\u4e00\u79cd\uff0c\u5373\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u6765\u8868\u793a\u6b63\u6574\u6570\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5185\u7f6e\u51fd\u6570\u5982 <code>int()<\/code> \u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u6216\u5176\u4ed6\u6570\u5b57\u7c7b\u578b\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32\u8868\u793a\u7684\u6570\u5b57 <code>&quot;123&quot;<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>int()<\/code> \u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num_str = &quot;123&quot;<\/p>\n<p>num = int(num_str)<\/p>\n<p>print(num)  # \u8f93\u51fa: 123<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u7528\u6237\u8f93\u5165\u6216\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u7684\u60c5\u5883\uff0c\u56e0\u4e3a\u8bfb\u53d6\u7684\u5185\u5bb9\u901a\u5e38\u4e3a\u5b57\u7b26\u4e32\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001\u76f4\u63a5\u8d4b\u503c\u6574\u6570<\/h2>\n<\/p>\n<p><p>\u76f4\u63a5\u8d4b\u503c\u6574\u6570\u662f\u6700\u5e38\u89c1\u4e14\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u3002\u5728Python\u4e2d\uff0c\u6574\u6570\u5b57\u9762\u91cf\u76f4\u63a5\u8868\u793a\u6b63\u6574\u6570\u3002\u4f8b\u5982\uff0c\u8d4b\u503c\u8bed\u53e5 <code>a = 5<\/code> \u5373\u8868\u793a <code>a<\/code> \u662f\u4e00\u4e2a\u6b63\u6574\u6570\u3002\u6211\u4eec\u901a\u8fc7\u51e0\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u6765\u8fdb\u4e00\u6b65\u7406\u89e3\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u7b80\u5355\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u8d4b\u503c\u662fPython\u4e2d\u6700\u57fa\u7840\u7684\u64cd\u4f5c\u4e4b\u4e00\u3002\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5c06\u4e00\u4e2a\u6b63\u6574\u6570\u8d4b\u503c\u7ed9\u53d8\u91cf\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>b = 123<\/p>\n<p>c = 456789<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u53d8\u91cf <code>a<\/code>\u3001<code>b<\/code> \u548c <code>c<\/code> \u5206\u522b\u8868\u793a\u6b63\u6574\u6570 10\u3001123 \u548c 456789\u3002Python\u5bf9\u53d8\u91cf\u7c7b\u578b\u6ca1\u6709\u4e25\u683c\u7684\u8981\u6c42\uff0c\u53d8\u91cf\u53ef\u4ee5\u968f\u65f6\u66f4\u6539\u5176\u503c\u548c\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u901a\u8fc7\u8fd0\u7b97\u7ed3\u679c\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u76f4\u63a5\u8d4b\u503c\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u8fd0\u7b97\u7ed3\u679c\u6765\u8d4b\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 5 + 3<\/p>\n<p>b = 2 * 6<\/p>\n<p>c = 12 \/\/ 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e9b\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf <code>a<\/code>\u3001<code>b<\/code> \u548c <code>c<\/code> \u5206\u522b\u88ab\u8d4b\u503c\u4e3a\u6b63\u6574\u6570 8\u300112 \u548c 6\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u5904\u7406\u548c\u8868\u793a\u6b63\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<\/h2>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u65b9\u4fbf\u5730\u5904\u7406\u548c\u8868\u793a\u6b63\u6574\u6570\u3002\u6700\u5e38\u7528\u7684\u5185\u7f6e\u51fd\u6570\u662f <code>int()<\/code>\uff0c\u5b83\u53ef\u4ee5\u5c06\u5176\u4ed6\u7c7b\u578b\u7684\u6570\u636e\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32\u8868\u793a\u7684\u6570\u5b57\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>int()<\/code> \u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num_str = &quot;123&quot;<\/p>\n<p>num = int(num_str)<\/p>\n<p>print(num)  # \u8f93\u51fa: 123<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u7528\u6237\u8f93\u5165\u6216\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u7684\u60c5\u5883\uff0c\u56e0\u4e3a\u8bfb\u53d6\u7684\u5185\u5bb9\u901a\u5e38\u4e3a\u5b57\u7b26\u4e32\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6574\u6570<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528 <code>int()<\/code> \u51fd\u6570\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">float_num = 123.45<\/p>\n<p>int_num = int(float_num)<\/p>\n<p>print(int_num)  # \u8f93\u51fa: 123<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>int()<\/code> \u51fd\u6570\u4f1a\u622a\u65ad\u5c0f\u6570\u90e8\u5206\uff0c\u800c\u4e0d\u662f\u8fdb\u884c\u56db\u820d\u4e94\u5165\u64cd\u4f5c\u3002\u8fd9\u610f\u5473\u7740 <code>123.45<\/code> \u4f1a\u88ab\u8f6c\u6362\u4e3a <code>123<\/code>\uff0c\u800c\u4e0d\u662f <code>124<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u901a\u8fc7\u8f93\u5165\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u6b63\u6574\u6570\u3002Python\u63d0\u4f9b\u4e86 <code>input()<\/code> \u51fd\u6570\uff0c\u53ef\u4ee5\u4ece\u7528\u6237\u90a3\u91cc\u83b7\u53d6\u8f93\u5165\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6b63\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u7b80\u5355\u7684\u7528\u6237\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>input()<\/code> \u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\uff0c\u7136\u540e\u4f7f\u7528 <code>int()<\/code> \u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u4f8b\u5982\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>num = int(user_input)<\/p>\n<p>print(f&quot;\u4f60\u8f93\u5165\u7684\u6b63\u6574\u6570\u662f: {num}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>input()<\/code> \u51fd\u6570\u4f1a\u6682\u505c\u7a0b\u5e8f\u6267\u884c\uff0c\u7b49\u5f85\u7528\u6237\u8f93\u5165\u5185\u5bb9\u3002\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u4f1a\u88ab\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fd4\u56de\uff0c\u7136\u540e\u901a\u8fc7 <code>int()<\/code> \u51fd\u6570\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u6dfb\u52a0\u8f93\u5165\u9a8c\u8bc1<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u786e\u5b9e\u662f\u4e00\u4e2a\u6b63\u6574\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0\u8f93\u5165\u9a8c\u8bc1\u3002\u4f8b\u5982\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>        num = int(user_input)<\/p>\n<p>        if num &gt; 0:<\/p>\n<p>            break<\/p>\n<p>        else:<\/p>\n<p>            print(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570!&quot;)<\/p>\n<p>    except ValueError:<\/p>\n<p>        print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570!&quot;)<\/p>\n<p>print(f&quot;\u4f60\u8f93\u5165\u7684\u6b63\u6574\u6570\u662f: {num}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a <code>while<\/code> \u5faa\u73af\u548c <code>try-except<\/code> \u8bed\u53e5\u6765\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u662f\u5426\u4e3a\u6b63\u6574\u6570\u3002\u5982\u679c\u8f93\u5165\u65e0\u6548\uff0c\u7a0b\u5e8f\u4f1a\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\uff0c\u76f4\u5230\u83b7\u5f97\u6709\u6548\u7684\u6b63\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u8868\u793a\u548c\u5904\u7406\u6b63\u6574\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u6211\u4eec\u7684\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\u548c\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5b9a\u4e49\u7b80\u5355\u7684\u6b63\u6574\u6570\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\u6765\u8868\u793a\u6b63\u6574\u6570\u3002\u4f8b\u5982\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>            num = int(user_input)<\/p>\n<p>            if num &gt; 0:<\/p>\n<p>                return num<\/p>\n<p>            else:<\/p>\n<p>                print(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570!&quot;)<\/p>\n<p>        except ValueError:<\/p>\n<p>            print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570!&quot;)<\/p>\n<p>num = get_positive_integer()<\/p>\n<p>print(f&quot;\u4f60\u8f93\u5165\u7684\u6b63\u6574\u6570\u662f: {num}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a <code>get_positive_integer()<\/code> \u7684\u51fd\u6570\uff0c\u7528\u4e8e\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u6b63\u6574\u6570\u3002\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u4e86 <code>while<\/code> \u5faa\u73af\u548c <code>try-except<\/code> \u8bed\u53e5\u6765\u9a8c\u8bc1\u8f93\u5165\uff0c\u5e76\u5728\u83b7\u5f97\u6709\u6548\u7684\u6b63\u6574\u6570\u540e\u8fd4\u56de\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u6269\u5c55\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u6269\u5c55\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u4f7f\u5176\u5177\u5907\u66f4\u591a\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0\u53c2\u6570\u6765\u6307\u5b9a\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_positive_integer(min_value=1, max_value=100):<\/p>\n<p>    while True:<\/p>\n<p>        user_input = input(f&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u4ecb\u4e8e {min_value} \u548c {max_value} \u4e4b\u95f4\u7684\u6b63\u6574\u6570: &quot;)<\/p>\n<p>        try:<\/p>\n<p>            num = int(user_input)<\/p>\n<p>            if min_value &lt;= num &lt;= max_value:<\/p>\n<p>                return num<\/p>\n<p>            else:<\/p>\n<p>                print(f&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u4ecb\u4e8e {min_value} \u548c {max_value} \u4e4b\u95f4\u7684\u6b63\u6574\u6570!&quot;)<\/p>\n<p>        except ValueError:<\/p>\n<p>            print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570!&quot;)<\/p>\n<p>num = get_positive_integer(10, 50)<\/p>\n<p>print(f&quot;\u4f60\u8f93\u5165\u7684\u6b63\u6574\u6570\u662f: {num}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u6269\u5c55\u4e86 <code>get_positive_integer()<\/code> \u51fd\u6570\uff0c\u4f7f\u5176\u53ef\u4ee5\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570 <code>min_value<\/code> \u548c <code>max_value<\/code>\uff0c\u5e76\u5728\u7528\u6237\u8f93\u5165\u7684\u6b63\u6574\u6570\u4e0d\u5728\u6307\u5b9a\u8303\u56f4\u5185\u65f6\u63d0\u793a\u91cd\u65b0\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u9a8c\u8bc1\u8f93\u5165<\/h2>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u9a8c\u8bc1\u5b57\u7b26\u4e32\u662f\u5426\u7b26\u5408\u7279\u5b9a\u7684\u683c\u5f0f\u3002\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>re<\/code> \u6a21\u5757\u6765\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u7b80\u5355\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u9a8c\u8bc1<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u662f\u5426\u4e3a\u6b63\u6574\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def is_positive_integer(s):<\/p>\n<p>    return re.match(r&quot;^[1-9]\\d*$&quot;, s) is not None<\/p>\n<p>user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570: &quot;)<\/p>\n<p>if is_positive_integer(user_input):<\/p>\n<p>    num = int(user_input)<\/p>\n<p>    print(f&quot;\u4f60\u8f93\u5165\u7684\u6b63\u6574\u6570\u662f: {num}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a <code>is_positive_integer()<\/code> \u7684\u51fd\u6570\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f <code>r&quot;^[1-9]\\d*$&quot;<\/code> \u6765\u9a8c\u8bc1\u8f93\u5165\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u6b63\u6574\u6570\u3002\u5982\u679c\u9a8c\u8bc1\u901a\u8fc7\uff0c\u5b57\u7b26\u4e32\u4f1a\u88ab\u8f6c\u6362\u4e3a\u6574\u6570\u5e76\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0e\u81ea\u5b9a\u4e49\u51fd\u6570\u7ed3\u5408\u8d77\u6765\uff0c\u7f16\u5199\u4e00\u4e2a\u66f4\u52a0\u5065\u58ee\u7684\u8f93\u5165\u9a8c\u8bc1\u51fd\u6570\u3002\u4f8b\u5982\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&quot;^[1-9]\\d*$&quot;, 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!&quot;)<\/p>\n<p>num = get_positive_integer()<\/p>\n<p>print(f&quot;\u4f60\u8f93\u5165\u7684\u6b63\u6574\u6570\u662f: {num}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u662f\u5426\u4e3a\u6b63\u6574\u6570\uff0c\u5e76\u5728\u83b7\u5f97\u6709\u6548\u7684\u6b63\u6574\u6570\u540e\u8fd4\u56de\u3002\u8fd9\u79cd\u65b9\u6cd5\u7ed3\u5408\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5f3a\u5927\u9a8c\u8bc1\u80fd\u529b\u548c\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u7075\u6d3b\u6027\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u5065\u58ee\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u5904\u7406\u5927\u6574\u6570<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6574\u6570\u7c7b\u578b\u6ca1\u6709\u56fa\u5b9a\u7684\u5927\u5c0f\u9650\u5236\uff0c\u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u5904\u7406\u975e\u5e38\u5927\u7684\u6b63\u6574\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><h3>1\u3001\u76f4\u63a5\u8d4b\u503c\u5927\u6574\u6570<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8d4b\u503c\u4e00\u4e2a\u975e\u5e38\u5927\u7684\u6b63\u6574\u6570\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">large_num = 123456789012345678901234567890<\/p>\n<p>print(large_num)  # \u8f93\u51fa: 123456789012345678901234567890<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Python\u4f1a\u81ea\u52a8\u5904\u7406\u5927\u6574\u6570\uff0c\u5e76\u4e14\u4e0d\u9700\u8981\u6211\u4eec\u8fdb\u884c\u989d\u5916\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u5904\u7406\u5927\u6574\u6570<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 <code>int()<\/code> \u5904\u7406\u5927\u6574\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">large_num_str = &quot;123456789012345678901234567890&quot;<\/p>\n<p>large_num = int(large_num_str)<\/p>\n<p>print(large_num)  # \u8f93\u51fa: 123456789012345678901234567890<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u6765\u81ea\u5916\u90e8\u6570\u636e\u6e90\u7684\u5927\u6574\u6570\uff0c\u4f8b\u5982\u4ece\u6587\u4ef6\u6216\u7f51\u7edc\u8bfb\u53d6\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5904\u7406\u7279\u6b8a\u6574\u6570<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u6765\u5904\u7406\u7279\u6b8a\u7684\u6574\u6570\u7c7b\u578b\u3002\u4f8b\u5982\uff0c<code>numpy<\/code> \u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\uff0c<code>decimal<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u9ad8\u7cbe\u5ea6\u7684\u6d6e\u70b9\u6570\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528 <code>numpy<\/code> \u5904\u7406\u5927\u6574\u6570\u6570\u7ec4<\/h3>\n<\/p>\n<p><p><code>numpy<\/code> \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>large_nums = np.array([1234567890123456789, 9876543210987654321], dtype=np.int64)<\/p>\n<p>print(large_nums)  # \u8f93\u51fa: [1234567890123456789 9876543210987654321]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>numpy<\/code> \u5141\u8bb8\u6211\u4eec\u5904\u7406\u5927\u6574\u6570\u6570\u7ec4\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528 <code>decimal<\/code> \u6a21\u5757\u5904\u7406\u9ad8\u7cbe\u5ea6\u6574\u6570<\/h3>\n<\/p>\n<p><p><code>decimal<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u9ad8\u7cbe\u5ea6\u7684\u6d6e\u70b9\u6570\u8fd0\u7b97\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from decimal import Decimal<\/p>\n<p>large_num = Decimal(&quot;123456789012345678901234567890&quot;)<\/p>\n<p>print(large_num)  # \u8f93\u51fa: 123456789012345678901234567890<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>decimal<\/code> \u6a21\u5757\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u7cbe\u5ea6\u8ba1\u7b97\u7684\u573a\u666f\uff0c\u4f8b\u5982\u91d1\u878d\u8ba1\u7b97\u548c\u79d1\u5b66\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u7528Python\u8868\u793a\u6b63\u6574\u6570\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u5305\u62ec\u76f4\u63a5\u8d4b\u503c\u6574\u6570\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u901a\u8fc7\u8f93\u5165\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u9a8c\u8bc1\u8f93\u5165\u3001\u5904\u7406\u5927\u6574\u6570\u548c\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5904\u7406\u7279\u6b8a\u6574\u6570\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u5e94\u7528\u573a\u666f\u548c\u4f18\u52bf\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4ee5\u5b9e\u73b0\u9ad8\u6548\u3001\u53ef\u9760\u7684\u6b63\u6574\u6570\u8868\u793a\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u8bfb\u8005\u5df2\u7ecf\u5bf9\u5982\u4f55\u7528Python\u8868\u793a\u6b63\u6574\u6570\u6709\u4e86\u5168\u9762\u7684\u4e86\u89e3\u548c\u638c\u63e1\u3002\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u6211\u4eec\u7684\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3001\u5065\u58ee\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u6b63\u6574\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6b63\u6574\u6570\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u8d4b\u503c\u6765\u5b9a\u4e49\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u5b9a\u4e49\u4e00\u4e2a\u6b63\u6574\u6570\uff1a  <\/p>\n<pre><code class=\"language-python\">number = 5  # \u8fd9\u662f\u4e00\u4e2a\u6b63\u6574\u6570\n<\/code><\/pre>\n<p>Python\u5141\u8bb8\u4f60\u76f4\u63a5\u8d4b\u503c\u4efb\u4f55\u6b63\u6574\u6570\u5230\u53d8\u91cf\u4e2d\uff0c\u65e0\u9700\u989d\u5916\u7684\u58f0\u660e\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u68c0\u67e5\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u6b63\u6574\u6570\uff1f<\/strong><br \/>\u8981\u68c0\u67e5\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u6b63\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u7ed3\u5408\u5185\u7f6e\u7684<code>isinstance()<\/code>\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">def is_positive_integer(num):\n    return isinstance(num, int) and num &gt; 0\n\nprint(is_positive_integer(10))  # \u8f93\u51fa\uff1aTrue\nprint(is_positive_integer(-5))  # \u8f93\u51fa\uff1aFalse\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u4f60\u4e0d\u4ec5\u68c0\u67e5\u6570\u503c\u7684\u6b63\u6027\uff0c\u8fd8\u786e\u8ba4\u5176\u6570\u636e\u7c7b\u578b\u4e3a\u6574\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u5927\u578b\u6b63\u6574\u6570\uff1f<\/strong><br \/>Python\u7684<code>int<\/code>\u7c7b\u578b\u80fd\u591f\u5904\u7406\u4efb\u610f\u5927\u5c0f\u7684\u6574\u6570\uff0c\u5305\u62ec\u975e\u5e38\u5927\u7684\u6b63\u6574\u6570\u3002\u53ef\u4ee5\u76f4\u63a5\u5c06\u5927\u6570\u5b57\u8d4b\u503c\u7ed9\u53d8\u91cf\u3002\u6bd4\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">large_number = 123456789012345678901234567890\nprint(large_number)\n<\/code><\/pre>\n<p>\u4e0d\u9700\u8981\u62c5\u5fc3\u6ea2\u51fa\u6216\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u7684\u95ee\u9898\uff0c\u56e0\u4e3aPython\u4f1a\u81ea\u52a8\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u8868\u793a\u6b63\u6574\u6570\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u6574\u6570\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u6216\u901a\u8fc7\u8f93\u5165\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\u3002\u5176\u4e2d\uff0c\u76f4\u63a5\u8d4b\u503c\u6574\u6570\u662f\u6700 [&hellip;]","protected":false},"author":3,"featured_media":1164877,"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\/1164868"}],"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=1164868"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1164868\/revisions"}],"predecessor-version":[{"id":1164878,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1164868\/revisions\/1164878"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1164877"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1164868"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1164868"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1164868"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}