{"id":1081694,"date":"2025-01-08T12:41:53","date_gmt":"2025-01-08T04:41:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1081694.html"},"modified":"2025-01-08T12:41:56","modified_gmt":"2025-01-08T04:41:56","slug":"python%e9%87%8c%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e8%be%93%e5%85%a5%e7%9a%84%e4%b8%ba%e6%95%b0%e5%ad%97-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1081694.html","title":{"rendered":"python\u91cc\u5982\u4f55\u5224\u65ad\u8f93\u5165\u7684\u4e3a\u6570\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183605\/8ab4b8f8-0fe5-4f8a-8e7c-c793898e414b.webp\" alt=\"python\u91cc\u5982\u4f55\u5224\u65ad\u8f93\u5165\u7684\u4e3a\u6570\u5b57\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5224\u65ad\u8f93\u5165\u662f\u5426\u4e3a\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528str.isdigit()\u65b9\u6cd5\u3001\u4f7f\u7528str.isnumeric()\u65b9\u6cd5\u3001\u4f7f\u7528str.isdecimal()\u65b9\u6cd5\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002<\/strong>\u5176\u4e2d\uff0cstr.isdigit()\u65b9\u6cd5\u662f\u6700\u5e38\u7528\u7684\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u53ea\u5305\u542b\u6570\u5b57\u5b57\u7b26\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528str.isdigit()\u65b9\u6cd5\u5224\u65ad\u8f93\u5165\u662f\u5426\u4e3a\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><p><strong>str.isdigit()\u65b9\u6cd5\uff1a<\/strong> \u8be5\u65b9\u6cd5\u7528\u4e8e\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u53ea\u5305\u542b\u6570\u5b57\u5b57\u7b26\uff080-9\uff09\u3002\u5982\u679c\u5b57\u7b26\u4e32\u53ea\u5305\u542b\u6570\u5b57\u5b57\u7b26\uff0c\u5219\u8fd4\u56deTrue\uff0c\u5426\u5219\u8fd4\u56deFalse\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">input_str = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32: &quot;)<\/p>\n<p>if input_str.isdigit():<\/p>\n<p>    print(&quot;\u8f93\u5165\u7684\u662f\u6570\u5b57\u3002&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u8f93\u5165\u7684\u4e0d\u662f\u6570\u5b57\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0cinput()\u51fd\u6570\u7528\u4e8e\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\uff0cisdigit()\u65b9\u6cd5\u7528\u4e8e\u5224\u65ad\u8be5\u5b57\u7b26\u4e32\u662f\u5426\u53ea\u5305\u542b\u6570\u5b57\u5b57\u7b26\u3002\u5982\u679c\u662f\uff0c\u5219\u8f93\u51fa\u201c\u8f93\u5165\u7684\u662f\u6570\u5b57\u3002\u201d\uff0c\u5426\u5219\u8f93\u51fa\u201c\u8f93\u5165\u7684\u4e0d\u662f\u6570\u5b57\u3002\u201d\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001str.isdigit()\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>str.isdigit()\u65b9\u6cd5\u662fPython\u4e2d\u7528\u4e8e\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u53ea\u5305\u542b\u6570\u5b57\u5b57\u7b26\u7684\u5e38\u7528\u65b9\u6cd5\u4e4b\u4e00\u3002\u8be5\u65b9\u6cd5\u53ea\u9002\u7528\u4e8e\u5224\u65ad\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u662f\u5426\u4e3a\u6570\u5b57\u5b57\u7b26\uff080-9\uff09\uff0c\u4e0d\u5305\u62ec\u5c0f\u6570\u70b9\u3001\u8d1f\u53f7\u7b49\u5176\u4ed6\u5b57\u7b26\u3002\u56e0\u6b64\uff0c\u8be5\u65b9\u6cd5\u9002\u7528\u4e8e\u5224\u65ad\u6574\u6570\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">input_str = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32: &quot;)<\/p>\n<p>if input_str.isdigit():<\/p>\n<p>    print(&quot;\u8f93\u5165\u7684\u662f\u6570\u5b57\u3002&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u8f93\u5165\u7684\u4e0d\u662f\u6570\u5b57\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u901a\u8fc7input()\u51fd\u6570\u83b7\u53d6\uff0cisdigit()\u65b9\u6cd5\u7528\u4e8e\u5224\u65ad\u8be5\u5b57\u7b26\u4e32\u662f\u5426\u53ea\u5305\u542b\u6570\u5b57\u5b57\u7b26\u3002\u5982\u679c\u662f\uff0c\u5219\u8f93\u51fa\u201c\u8f93\u5165\u7684\u662f\u6570\u5b57\u3002\u201d\uff0c\u5426\u5219\u8f93\u51fa\u201c\u8f93\u5165\u7684\u4e0d\u662f\u6570\u5b57\u3002\u201d\u3002<\/p>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cisdigit()\u65b9\u6cd5\u4e0d\u9002\u7528\u4e8e\u5224\u65ad\u5c0f\u6570\u6216\u8d1f\u6570\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u8fdb\u884c\u5224\u65ad\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e8c\u3001str.isnumeric()\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>str.isnumeric()\u65b9\u6cd5\u7528\u4e8e\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u53ea\u5305\u542b\u6570\u5b57\u5b57\u7b26\uff0c\u5305\u62ec\u6574\u6570\u3001\u5206\u6570\u3001\u6307\u6570\u7b49\u3002\u8be5\u65b9\u6cd5\u9002\u7528\u4e8e\u5224\u65ad\u66f4\u5e7f\u6cdb\u7684\u6570\u5b57\u5f62\u5f0f\uff0c\u4f46\u4e0d\u5305\u62ec\u5c0f\u6570\u70b9\u3001\u8d1f\u53f7\u7b49\u5176\u4ed6\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">input_str = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32: &quot;)<\/p>\n<p>if input_str.isnumeric():<\/p>\n<p>    print(&quot;\u8f93\u5165\u7684\u662f\u6570\u5b57\u3002&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u8f93\u5165\u7684\u4e0d\u662f\u6570\u5b57\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u901a\u8fc7input()\u51fd\u6570\u83b7\u53d6\uff0cisnumeric()\u65b9\u6cd5\u7528\u4e8e\u5224\u65ad\u8be5\u5b57\u7b26\u4e32\u662f\u5426\u53ea\u5305\u542b\u6570\u5b57\u5b57\u7b26\u3002\u5982\u679c\u662f\uff0c\u5219\u8f93\u51fa\u201c\u8f93\u5165\u7684\u662f\u6570\u5b57\u3002\u201d\uff0c\u5426\u5219\u8f93\u51fa\u201c\u8f93\u5165\u7684\u4e0d\u662f\u6570\u5b57\u3002\u201d\u3002<\/p>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cisnumeric()\u65b9\u6cd5\u4e0d\u9002\u7528\u4e8e\u5224\u65ad\u5c0f\u6570\u6216\u8d1f\u6570\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u8fdb\u884c\u5224\u65ad\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e09\u3001str.isdecimal()\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>str.isdecimal()\u65b9\u6cd5\u7528\u4e8e\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u53ea\u5305\u542b\u5341\u8fdb\u5236\u6570\u5b57\u5b57\u7b26\uff080-9\uff09\uff0c\u4e0d\u5305\u62ec\u5c0f\u6570\u70b9\u3001\u8d1f\u53f7\u7b49\u5176\u4ed6\u5b57\u7b26\u3002\u8be5\u65b9\u6cd5\u9002\u7528\u4e8e\u5224\u65ad\u6574\u6570\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">input_str = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32: &quot;)<\/p>\n<p>if input_str.isdecimal():<\/p>\n<p>    print(&quot;\u8f93\u5165\u7684\u662f\u6570\u5b57\u3002&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u8f93\u5165\u7684\u4e0d\u662f\u6570\u5b57\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u901a\u8fc7input()\u51fd\u6570\u83b7\u53d6\uff0cisdecimal()\u65b9\u6cd5\u7528\u4e8e\u5224\u65ad\u8be5\u5b57\u7b26\u4e32\u662f\u5426\u53ea\u5305\u542b\u5341\u8fdb\u5236\u6570\u5b57\u5b57\u7b26\u3002\u5982\u679c\u662f\uff0c\u5219\u8f93\u51fa\u201c\u8f93\u5165\u7684\u662f\u6570\u5b57\u3002\u201d\uff0c\u5426\u5219\u8f93\u51fa\u201c\u8f93\u5165\u7684\u4e0d\u662f\u6570\u5b57\u3002\u201d\u3002<\/p>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cisdecimal()\u65b9\u6cd5\u4e0d\u9002\u7528\u4e8e\u5224\u65ad\u5c0f\u6570\u6216\u8d1f\u6570\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u8fdb\u884c\u5224\u65ad\u3002<\/p>\n<\/p>\n<p><p><strong>\u56db\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5339\u914d\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u5224\u65ad\u8f93\u5165\u662f\u5426\u4e3a\u6570\u5b57\u3002\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u5224\u65ad\u6574\u6570\u3001\u5c0f\u6570\u3001\u8d1f\u6570\u7b49\u591a\u79cd\u6570\u5b57\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>input_str = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32: &quot;)<\/p>\n<p>if re.match(r&#39;^-?\\d+(\\.\\d+)?$&#39;, input_str):<\/p>\n<p>    print(&quot;\u8f93\u5165\u7684\u662f\u6570\u5b57\u3002&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u8f93\u5165\u7684\u4e0d\u662f\u6570\u5b57\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cre.match()\u51fd\u6570\u7528\u4e8e\u5339\u914d\u8f93\u5165\u5b57\u7b26\u4e32\u662f\u5426\u7b26\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0fr&#39;^-?\\d+(.\\d+)?$&#39;\u3002\u8be5\u6a21\u5f0f\u7528\u4e8e\u5339\u914d\u6574\u6570\u3001\u5c0f\u6570\u548c\u8d1f\u6570\u3002\u5982\u679c\u5339\u914d\u6210\u529f\uff0c\u5219\u8f93\u51fa\u201c\u8f93\u5165\u7684\u662f\u6570\u5b57\u3002\u201d\uff0c\u5426\u5219\u8f93\u51fa\u201c\u8f93\u5165\u7684\u4e0d\u662f\u6570\u5b57\u3002\u201d\u3002<\/p>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u6a21\u5f0f\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u8c03\u6574\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u7684\u6570\u5b57\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e94\u3001\u4f7f\u7528try-except\u8bed\u53e5<\/strong><\/p>\n<\/p>\n<p><p>try-except\u8bed\u53e5\u662fPython\u4e2d\u7528\u4e8e\u5f02\u5e38\u5904\u7406\u7684\u5e38\u7528\u8bed\u53e5\u3002\u901a\u8fc7try-except\u8bed\u53e5\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u5c06\u8f93\u5165\u8f6c\u6362\u4e3a\u6570\u5b57\u7c7b\u578b\uff0c\u5982\u679c\u8f6c\u6362\u6210\u529f\uff0c\u5219\u8868\u793a\u8f93\u5165\u4e3a\u6570\u5b57\uff1b\u5982\u679c\u8f6c\u6362\u5931\u8d25\uff0c\u5219\u8868\u793a\u8f93\u5165\u4e0d\u662f\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">input_str = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32: &quot;)<\/p>\n<p>try:<\/p>\n<p>    float(input_str)<\/p>\n<p>    print(&quot;\u8f93\u5165\u7684\u662f\u6570\u5b57\u3002&quot;)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;\u8f93\u5165\u7684\u4e0d\u662f\u6570\u5b57\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5c1d\u8bd5\u5c06\u8f93\u5165\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u7c7b\u578b\u3002\u5982\u679c\u8f6c\u6362\u6210\u529f\uff0c\u5219\u8f93\u51fa\u201c\u8f93\u5165\u7684\u662f\u6570\u5b57\u3002\u201d\uff1b\u5982\u679c\u8f6c\u6362\u5931\u8d25\uff0c\u5219\u6355\u83b7ValueError\u5f02\u5e38\uff0c\u5e76\u8f93\u51fa\u201c\u8f93\u5165\u7684\u4e0d\u662f\u6570\u5b57\u3002\u201d\u3002<\/p>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4f7f\u7528try-except\u8bed\u53e5\u53ef\u4ee5\u5224\u65ad\u6574\u6570\u3001\u5c0f\u6570\u548c\u8d1f\u6570\u7b49\u591a\u79cd\u6570\u5b57\u5f62\u5f0f\uff0c\u4f46\u4e0d\u80fd\u5224\u65ad\u5176\u4ed6\u975e\u6570\u5b57\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p><strong>\u516d\u3001\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5224\u65ad\u8f93\u5165\u662f\u5426\u4e3a\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>str.isdigit()\u65b9\u6cd5<\/strong>\uff1a\u7528\u4e8e\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u53ea\u5305\u542b\u6570\u5b57\u5b57\u7b26\uff080-9\uff09\uff0c\u9002\u7528\u4e8e\u5224\u65ad\u6574\u6570\u8f93\u5165\u3002<\/li>\n<li><strong>str.isnumeric()\u65b9\u6cd5<\/strong>\uff1a\u7528\u4e8e\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u53ea\u5305\u542b\u6570\u5b57\u5b57\u7b26\uff0c\u5305\u62ec\u6574\u6570\u3001\u5206\u6570\u3001\u6307\u6570\u7b49\uff0c\u4f46\u4e0d\u5305\u62ec\u5c0f\u6570\u70b9\u3001\u8d1f\u53f7\u7b49\u5176\u4ed6\u5b57\u7b26\u3002<\/li>\n<li><strong>str.isdecimal()\u65b9\u6cd5<\/strong>\uff1a\u7528\u4e8e\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u53ea\u5305\u542b\u5341\u8fdb\u5236\u6570\u5b57\u5b57\u7b26\uff080-9\uff09\uff0c\u9002\u7528\u4e8e\u5224\u65ad\u6574\u6570\u8f93\u5165\u3002<\/li>\n<li><strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\uff1a\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u5224\u65ad\u8f93\u5165\u662f\u5426\u4e3a\u6574\u6570\u3001\u5c0f\u6570\u3001\u8d1f\u6570\u7b49\u591a\u79cd\u6570\u5b57\u5f62\u5f0f\u3002<\/li>\n<li><strong>\u4f7f\u7528try-except\u8bed\u53e5<\/strong>\uff1a\u901a\u8fc7try-except\u8bed\u53e5\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u5c06\u8f93\u5165\u8f6c\u6362\u4e3a\u6570\u5b57\u7c7b\u578b\uff0c\u5982\u679c\u8f6c\u6362\u6210\u529f\uff0c\u5219\u8868\u793a\u8f93\u5165\u4e3a\u6570\u5b57\uff1b\u5982\u679c\u8f6c\u6362\u5931\u8d25\uff0c\u5219\u8868\u793a\u8f93\u5165\u4e0d\u662f\u6570\u5b57\u3002<\/li>\n<\/ol>\n<p><p>\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5224\u65ad\u8f93\u5165\u662f\u5426\u4e3a\u6570\u5b57\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u662fstr.isdigit()\u65b9\u6cd5\u548ctry-except\u8bed\u53e5\uff0c\u56e0\u4e3a\u5b83\u4eec\u9002\u7528\u8303\u56f4\u5e7f\u6cdb\uff0c\u4f7f\u7528\u7b80\u5355\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a\u6570\u5b57\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>str.isdigit()<\/code>\u65b9\u6cd5\u6765\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u53ea\u5305\u542b\u6570\u5b57\u5b57\u7b26\u3002\u5982\u679c\u60f3\u5224\u65ad\u66f4\u5e7f\u6cdb\u7684\u6570\u5b57\u683c\u5f0f\uff08\u5305\u62ec\u8d1f\u6570\u548c\u5c0f\u6570\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u7ed3\u5408<code>float()<\/code>\u6216<code>int()<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">def is_number(s):\n    try:\n        float(s)  # \u5c1d\u8bd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\n        return True\n    except ValueError:\n        return False\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u6709\u4ec0\u4e48\u5185\u5efa\u51fd\u6570\u53ef\u4ee5\u5224\u65ad\u8f93\u5165\u662f\u6570\u5b57\u5417\uff1f<\/strong><br \/>Python\u6ca1\u6709\u76f4\u63a5\u7684\u5185\u5efa\u51fd\u6570\u6765\u5224\u65ad\u4e00\u4e2a\u8f93\u5165\u662f\u5426\u4e3a\u6570\u5b57\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528<code>isinstance()<\/code>\u51fd\u6570\u914d\u5408\u7c7b\u578b\u68c0\u67e5\u3002\u5bf9\u4e8e\u8f93\u5165\u6570\u636e\uff0c\u5982\u679c\u7c7b\u578b\u662f<code>int<\/code>\u6216<code>float<\/code>\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u8ba4\u4e3a\u5b83\u662f\u6570\u5b57\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">def check_type(input_value):\n    return isinstance(input_value, (int, float))\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u975e\u6570\u5b57\u6570\u636e\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u6355\u83b7\u7528\u6237\u8f93\u5165\u7684\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u5f53\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u65e0\u6cd5\u8f6c\u6362\u4e3a\u6570\u5b57\u7684\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u4ee5\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">while True:\n    user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff1a&quot;)\n    if is_number(user_input):\n        print(&quot;\u60a8\u8f93\u5165\u7684\u6570\u5b57\u662f\uff1a&quot;, user_input)\n        break\n    else:\n        print(&quot;\u65e0\u6548\u8f93\u5165\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6709\u6548\u7684\u6570\u5b57\u3002&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5224\u65ad\u548c\u5904\u7406\u7528\u6237\u7684\u6570\u5b57\u8f93\u5165\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5224\u65ad\u8f93\u5165\u662f\u5426\u4e3a\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528str.isdigit()\u65b9\u6cd5\u3001\u4f7f\u7528str.is [&hellip;]","protected":false},"author":3,"featured_media":1081698,"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\/1081694"}],"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=1081694"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1081694\/revisions"}],"predecessor-version":[{"id":1081699,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1081694\/revisions\/1081699"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1081698"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1081694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1081694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1081694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}