{"id":1142153,"date":"2025-01-08T22:37:17","date_gmt":"2025-01-08T14:37:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1142153.html"},"modified":"2025-01-08T22:37:19","modified_gmt":"2025-01-08T14:37:19","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba%e4%b8%80%e4%b8%aa%e6%95%b0%e6%98%af%e6%ad%a3%e6%95%b4%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1142153.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8868\u793a\u4e00\u4e2a\u6570\u662f\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\/24180434\/92c71309-58a0-4bbc-a6e2-c8d132d6ee12.webp\" alt=\"python\u4e2d\u5982\u4f55\u8868\u793a\u4e00\u4e2a\u6570\u662f\u6b63\u6574\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u6b63\u6574\u6570\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u548c\u5185\u5efa\u51fd\u6570\u3001\u7c7b\u578b\u68c0\u67e5\u3001\u4ee5\u53ca\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/strong>\u5176\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u7ed3\u5408\u6761\u4ef6\u8bed\u53e5\u548c\u7c7b\u578b\u68c0\u67e5\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u76f4\u89c2\uff0c\u800c\u4e14\u6613\u4e8e\u7406\u89e3\u548c\u5b9e\u73b0\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u4e0d\u540c\u7684\u65b9\u6cd5\u53ca\u5176\u5b9e\u73b0\u65b9\u5f0f\uff0c\u4ee5\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6761\u4ef6\u8bed\u53e5\u548c\u5185\u5efa\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u548c\u5185\u5efa\u51fd\u6570\u662f\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u6b63\u6574\u6570\u7684\u6700\u76f4\u63a5\u65b9\u6cd5\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>if<\/code>\u8bed\u53e5\u7ed3\u5408<code>isinstance()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_positive_integer(n):<\/p>\n<p>    if isinstance(n, int) and n &gt; 0:<\/p>\n<p>        return True<\/p>\n<p>    return False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c<code>isinstance(n, int)<\/code>\u7528\u4e8e\u68c0\u67e5<code>n<\/code>\u662f\u5426\u4e3a\u6574\u6570\uff0c\u800c<code>n &gt; 0<\/code>\u5219\u7528\u4e8e\u5224\u65ad<code>n<\/code>\u662f\u5426\u4e3a\u6b63\u6570\u3002\u53ea\u6709\u5f53\u8fd9\u4e24\u4e2a\u6761\u4ef6\u540c\u65f6\u6ee1\u8db3\u65f6\uff0c\u51fd\u6570\u624d\u4f1a\u8fd4\u56de<code>True<\/code>\uff0c\u5426\u5219\u8fd4\u56de<code>False<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u7c7b\u578b\u68c0\u67e5<\/h3>\n<\/p>\n<p><p>\u7c7b\u578b\u68c0\u67e5\u662f\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u6b63\u6574\u6570\u7684\u53e6\u4e00\u79cd\u65b9\u6cd5\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>type()<\/code>\u51fd\u6570\u6216<code>isinstance()<\/code>\u51fd\u6570\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528<code>type()<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def is_positive_integer(n):<\/p>\n<p>    if type(n) == int and n &gt; 0:<\/p>\n<p>        return True<\/p>\n<p>    return False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0e<code>isinstance()<\/code>\u76f8\u6bd4\uff0c<code>type()<\/code>\u51fd\u6570\u53ea\u80fd\u68c0\u67e5\u4e00\u4e2a\u53d8\u91cf\u7684\u786e\u5207\u7c7b\u578b\uff0c\u800c\u4e0d\u80fd\u68c0\u67e5\u5176\u662f\u5426\u4e3a\u67d0\u4e2a\u7c7b\u578b\u7684\u5b50\u7c7b\u3002\u56e0\u6b64\uff0c<code>type()<\/code>\u51fd\u6570\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u4e0d\u5982<code>isinstance()<\/code>\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528<code>isinstance()<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def is_positive_integer(n):<\/p>\n<p>    if isinstance(n, int) and n &gt; 0:<\/p>\n<p>        return True<\/p>\n<p>    return False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>isinstance()<\/code>\u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u68c0\u67e5\u4e00\u4e2a\u53d8\u91cf\u7684\u786e\u5207\u7c7b\u578b\uff0c\u8fd8\u53ef\u4ee5\u68c0\u67e5\u5176\u662f\u5426\u4e3a\u67d0\u4e2a\u7c7b\u578b\u7684\u5b50\u7c7b\u3002\u56e0\u6b64\uff0c<code>isinstance()<\/code>\u51fd\u6570\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u66f4\u4e3a\u63a8\u8350\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u6b63\u6574\u6570\u7684\u53e6\u4e00\u79cd\u65b9\u6cd5\uff0c\u5c24\u5176\u5728\u5904\u7406\u5b57\u7b26\u4e32\u8f93\u5165\u65f6\u975e\u5e38\u6709\u7528\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def is_positive_integer(s):<\/p>\n<p>    return bool(re.match(r&#39;^[1-9]\\d*$&#39;, s))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f<code>^[1-9]\\d*$<\/code>\u7528\u4e8e\u5339\u914d\u6240\u6709\u4ee5\u975e\u96f6\u6570\u5b57\u5f00\u5934\u5e76\u4e14\u53ea\u5305\u542b\u6570\u5b57\u7684\u5b57\u7b26\u4e32\u3002\u5982\u679c\u5b57\u7b26\u4e32\u5339\u914d\u6210\u529f\uff0c<code>re.match()<\/code>\u51fd\u6570\u5c06\u8fd4\u56de\u4e00\u4e2a\u5339\u914d\u5bf9\u8c61\uff0c\u5426\u5219\u8fd4\u56de<code>None<\/code>\u3002<code>bool()<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u5339\u914d\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff0c\u4ee5\u4fbf\u51fd\u6570\u8fd4\u56de<code>True<\/code>\u6216<code>False<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u8003\u8651<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u6b63\u6574\u6570\u53ef\u80fd\u6d89\u53ca\u66f4\u591a\u7684\u8003\u8651\u56e0\u7d20\uff0c\u5982\u8f93\u5165\u6570\u636e\u7684\u7c7b\u578b\u3001\u8303\u56f4\u4ee5\u53ca\u6f5c\u5728\u7684\u5f02\u5e38\u60c5\u51b5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u548c\u89e3\u51b3\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>\u5904\u7406\u7528\u6237\u8f93\u5165<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\uff0c\u8f93\u5165\u6570\u636e\u901a\u5e38\u4e3a\u5b57\u7b26\u4e32\u3002\u56e0\u6b64\uff0c\u9700\u8981\u5148\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u7136\u540e\u518d\u8fdb\u884c\u5224\u65ad\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_positive_integer(s):<\/p>\n<p>    try:<\/p>\n<p>        n = int(s)<\/p>\n<p>        return isinstance(n, int) and n &gt; 0<\/p>\n<p>    except ValueError:<\/p>\n<p>        return False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c<code>try<\/code>\u8bed\u53e5\u7528\u4e8e\u6355\u83b7<code>int()<\/code>\u51fd\u6570\u53ef\u80fd\u5f15\u53d1\u7684<code>ValueError<\/code>\u5f02\u5e38\u3002\u5982\u679c\u5b57\u7b26\u4e32\u65e0\u6cd5\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u51fd\u6570\u5c06\u8fd4\u56de<code>False<\/code>\u3002<\/p>\n<\/p>\n<p><h4>\u5904\u7406\u6d6e\u70b9\u6570<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u5e94\u7528\u4e2d\uff0c\u8f93\u5165\u6570\u636e\u53ef\u80fd\u4e3a\u6d6e\u70b9\u6570\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5148\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u7136\u540e\u518d\u8fdb\u884c\u5224\u65ad\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_positive_integer(n):<\/p>\n<p>    if isinstance(n, float) and n.is_integer():<\/p>\n<p>        n = int(n)<\/p>\n<p>    return isinstance(n, int) and n &gt; 0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c<code>n.is_integer()<\/code>\u7528\u4e8e\u68c0\u67e5\u6d6e\u70b9\u6570\u662f\u5426\u4e3a\u6574\u6570\u3002\u5982\u679c\u662f\uff0c\u5219\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u7136\u540e\u518d\u8fdb\u884c\u5224\u65ad\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7efc\u5408\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7efc\u5408\u5b9e\u4f8b\uff0c\u7ed3\u5408\u4e86\u4e0a\u8ff0\u65b9\u6cd5\u548c\u8003\u8651\u56e0\u7d20\uff0c\u4ee5\u5b9e\u73b0\u66f4\u4e3a\u5168\u9762\u7684\u5224\u65ad\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def is_positive_integer(value):<\/p>\n<p>    # Check if the input is a string<\/p>\n<p>    if isinstance(value, str):<\/p>\n<p>        # Use regular expression to check if the string represents a positive integer<\/p>\n<p>        if re.match(r&#39;^[1-9]\\d*$&#39;, value):<\/p>\n<p>            return True<\/p>\n<p>        else:<\/p>\n<p>            return False<\/p>\n<p>    # Check if the input is a float and if it represents an integer<\/p>\n<p>    elif isinstance(value, float) and value.is_integer():<\/p>\n<p>        value = int(value)<\/p>\n<p>    # Check if the input is an integer and greater than zero<\/p>\n<p>    if isinstance(value, int) and value &gt; 0:<\/p>\n<p>        return True<\/p>\n<p>    return False<\/p>\n<h2><strong>Test the function with different types of input<\/strong><\/h2>\n<p>print(is_positive_integer(&quot;123&quot;))  # True<\/p>\n<p>print(is_positive_integer(&quot;-123&quot;))  # False<\/p>\n<p>print(is_positive_integer(&quot;abc&quot;))  # False<\/p>\n<p>print(is_positive_integer(123))  # True<\/p>\n<p>print(is_positive_integer(-123))  # False<\/p>\n<p>print(is_positive_integer(123.0))  # True<\/p>\n<p>print(is_positive_integer(123.45))  # False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u5b9e\u4f8b\u4e2d\uff0c<code>is_positive_integer()<\/code>\u51fd\u6570\u80fd\u591f\u5904\u7406\u5b57\u7b26\u4e32\u3001\u6574\u6570\u548c\u6d6e\u70b9\u6570\u8f93\u5165\uff0c\u5e76\u51c6\u786e\u5224\u65ad\u5176\u662f\u5426\u4e3a\u6b63\u6574\u6570\u3002\u8fd9\u79cd\u7efc\u5408\u65b9\u6cd5\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u66f4\u4e3a\u5b9e\u7528\u548c\u53ef\u9760\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\u8d77\u6765\uff0c\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u6b63\u6574\u6570\u5728Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec\u6761\u4ef6\u8bed\u53e5\u548c\u5185\u5efa\u51fd\u6570\u3001\u7c7b\u578b\u68c0\u67e5\u4ee5\u53ca\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u6839\u636e\u5177\u4f53\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u6b63\u6574\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6765\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u6b63\u6574\u6570\u3002\u53ef\u4ee5\u901a\u8fc7\u6bd4\u8f83\u8be5\u6570\u662f\u5426\u5927\u4e8e\u96f6\u5e76\u4e14\u662f\u6574\u6570\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>isinstance()<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u8be5\u6570\u7684\u7c7b\u578b\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">def is_positive_integer(num):\n    return isinstance(num, int) and num &gt; 0\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u4f1a\u8fd4\u56de<code>True<\/code>\u5982\u679c<code>num<\/code>\u662f\u6b63\u6574\u6570\uff0c\u8fd4\u56de<code>False<\/code>\u5426\u5219\u3002<\/p>\n<p><strong>Python\u4e2d\u6b63\u6574\u6570\u7684\u8303\u56f4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6b63\u6574\u6570\u7684\u8303\u56f4\u5e76\u6ca1\u6709\u56fa\u5b9a\u7684\u4e0a\u9650\uff0c\u56e0\u4e3aPython\u7684\u6574\u6570\u7c7b\u578b\u53ef\u4ee5\u6839\u636e\u9700\u8981\u52a8\u6001\u6269\u5c55\u3002\u53ea\u8981\u6709\u8db3\u591f\u7684\u5185\u5b58\uff0c\u60a8\u53ef\u4ee5\u8868\u793a\u4efb\u610f\u5927\u5c0f\u7684\u6b63\u6574\u6570\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u8d1f\u6570\u548c\u96f6\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u8981\u5904\u7406\u8d1f\u6570\u548c\u96f6\uff0c\u53ef\u4ee5\u5728\u5224\u65ad\u6b63\u6574\u6570\u7684\u6761\u4ef6\u4e2d\u52a0\u5165\u989d\u5916\u7684\u903b\u8f91\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>if<\/code>\u8bed\u53e5\u6765\u5206\u522b\u8f93\u51fa\u8d1f\u6570\u548c\u96f6\u7684\u63d0\u793a\u4fe1\u606f\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">num = -5\nif num &lt;= 0:\n    print(&quot;\u8be5\u6570\u4e0d\u662f\u6b63\u6574\u6570&quot;)\nelse:\n    print(&quot;\u8be5\u6570\u662f\u6b63\u6574\u6570&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u7528\u6237\u6e05\u695a\u6570\u503c\u7684\u72b6\u6001\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u6b63\u6574\u6570\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u548c\u5185\u5efa\u51fd\u6570\u3001\u7c7b\u578b\u68c0\u67e5\u3001\u4ee5\u53ca\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u5176\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1142163,"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\/1142153"}],"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=1142153"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1142153\/revisions"}],"predecessor-version":[{"id":1142166,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1142153\/revisions\/1142166"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1142163"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1142153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1142153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1142153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}