{"id":1094842,"date":"2025-01-08T14:45:03","date_gmt":"2025-01-08T06:45:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1094842.html"},"modified":"2025-01-08T14:45:05","modified_gmt":"2025-01-08T06:45:05","slug":"python%e7%9a%84%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e8%be%93%e5%85%a5%e6%98%af%e5%90%a6%e6%95%b0%e5%ad%97%e7%b1%bb%e5%9e%8b-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1094842.html","title":{"rendered":"python\u7684\u5982\u4f55\u5224\u65ad\u8f93\u5165\u662f\u5426\u6570\u5b57\u7c7b\u578b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210745\/e595c1b3-30b8-40b5-9a6d-67ce8cdb0800.webp\" alt=\"python\u7684\u5982\u4f55\u5224\u65ad\u8f93\u5165\u662f\u5426\u6570\u5b57\u7c7b\u578b\" \/><\/p>\n<p><p> <strong>\u8981\u5224\u65adPython\u4e2d\u7684\u8f93\u5165\u662f\u5426\u4e3a\u6570\u5b57\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1astr.isdigit()\u3001try-except\u6355\u83b7\u5f02\u5e38\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002<\/strong> \u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4f7f\u7528try-except\u6355\u83b7\u5f02\u5e38\u7684\u65b9\u6cd5\u662f\u6700\u5e38\u7528\u4e14\u7075\u6d3b\u7684\uff0c\u56e0\u4e3a\u5b83\u80fd\u5904\u7406\u6574\u6570\u548c\u6d6e\u70b9\u6570\u3002\u5728\u8fd9\u4e00\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u5c55\u5f00try-except\u6355\u83b7\u5f02\u5e38\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>try-except\u6355\u83b7\u5f02\u5e38\u7684\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\u4e14\u6709\u6548\u3002\u5b83\u7684\u57fa\u672c\u601d\u60f3\u662f\u5c1d\u8bd5\u5c06\u8f93\u5165\u8f6c\u6362\u4e3a\u6570\u5b57\u7c7b\u578b\uff0c\u5982\u679c\u8f6c\u6362\u6210\u529f\uff0c\u5219\u8f93\u5165\u4e3a\u6570\u5b57\u7c7b\u578b\uff1b\u5982\u679c\u629b\u51fa\u5f02\u5e38\uff0c\u5219\u8f93\u5165\u4e0d\u662f\u6570\u5b57\u7c7b\u578b\u3002\u4ee5\u4e0b\u662ftry-except\u6355\u83b7\u5f02\u5e38\u65b9\u6cd5\u7684\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_number(s):<\/p>\n<p>    try:<\/p>\n<p>        float(s)<\/p>\n<p>        return True<\/p>\n<p>    except ValueError:<\/p>\n<p>        return False<\/p>\n<p>input_value = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u503c\uff1a&quot;)<\/p>\n<p>if is_number(input_value):<\/p>\n<p>    print(f&quot;{input_value} \u662f\u4e00\u4e2a\u6570\u5b57\u3002&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{input_value} \u4e0d\u662f\u4e00\u4e2a\u6570\u5b57\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u51fd\u6570<code>is_number<\/code>\u5c1d\u8bd5\u5c06\u8f93\u5165\u53c2\u6570<code>s<\/code>\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002\u5982\u679c\u8f6c\u6362\u6210\u529f\uff0c\u51fd\u6570\u8fd4\u56deTrue\uff0c\u8868\u793a\u8f93\u5165\u662f\u6570\u5b57\u7c7b\u578b\uff1b\u5982\u679c\u629b\u51faValueError\u5f02\u5e38\uff0c\u51fd\u6570\u8fd4\u56deFalse\uff0c\u8868\u793a\u8f93\u5165\u4e0d\u662f\u6570\u5b57\u7c7b\u578b\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u66f4\u6df1\u5165\u5730\u63a2\u8ba8\u5176\u4ed6\u65b9\u6cd5\u4ee5\u53ca\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001str.isdigit()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u4ecb\u7ecd<\/h4>\n<\/p>\n<p><p><code>str.isdigit()<\/code>\u65b9\u6cd5\u662fPython\u5185\u7f6e\u5b57\u7b26\u4e32\u65b9\u6cd5\u4e4b\u4e00\uff0c\u7528\u4e8e\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u53ea\u5305\u542b\u6570\u5b57\u5b57\u7b26\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff1a\u5982\u679c\u5b57\u7b26\u4e32\u53ea\u5305\u542b\u6570\u5b57\u5b57\u7b26\uff0c\u8fd4\u56deTrue\uff1b\u5426\u5219\uff0c\u8fd4\u56deFalse\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f18\u70b9\u548c\u5c40\u9650\u6027<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>str.isdigit()<\/code>\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u5b83\u975e\u5e38\u7b80\u5355\u6613\u7528\u3002\u7136\u800c\uff0c\u5b83\u6709\u4e00\u4e9b\u5c40\u9650\u6027\uff1a\u5b83\u53ea\u80fd\u5224\u65ad\u6574\u6570\uff0c\u4e0d\u80fd\u8bc6\u522b\u6d6e\u70b9\u6570\u6216\u8d1f\u6570\u3002\u4f8b\u5982\uff0c\u5b57\u7b26\u4e32\u201c123\u201d\u4f1a\u8fd4\u56deTrue\uff0c\u4f46\u5b57\u7b26\u4e32\u201c123.45\u201d\u6216\u201c-123\u201d\u4f1a\u8fd4\u56deFalse\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u4ee3\u7801\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">input_value = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u503c\uff1a&quot;)<\/p>\n<p>if input_value.isdigit():<\/p>\n<p>    print(f&quot;{input_value} \u662f\u4e00\u4e2a\u6574\u6570\u3002&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{input_value} \u4e0d\u662f\u4e00\u4e2a\u6574\u6570\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001try-except\u6355\u83b7\u5f02\u5e38<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u4ecb\u7ecd<\/h4>\n<\/p>\n<p><p>try-except\u6355\u83b7\u5f02\u5e38\u7684\u65b9\u6cd5\u5229\u7528Python\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u5c1d\u8bd5\u5c06\u8f93\u5165\u8f6c\u6362\u4e3a\u6570\u5b57\u7c7b\u578b\u3002\u5982\u679c\u8f6c\u6362\u6210\u529f\uff0c\u5219\u8f93\u5165\u4e3a\u6570\u5b57\u7c7b\u578b\uff1b\u5982\u679c\u629b\u51fa\u5f02\u5e38\uff0c\u5219\u8f93\u5165\u4e0d\u662f\u6570\u5b57\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f18\u70b9\u548c\u5c40\u9650\u6027<\/h4>\n<\/p>\n<p><p>try-except\u6355\u83b7\u5f02\u5e38\u7684\u65b9\u6cd5\u975e\u5e38\u7075\u6d3b\uff0c\u80fd\u591f\u5904\u7406\u6574\u6570\u3001\u6d6e\u70b9\u6570\u548c\u8d1f\u6570\u7b49\u5404\u79cd\u60c5\u51b5\u3002\u5176\u4e3b\u8981\u4f18\u70b9\u662f\u80fd\u591f\u5904\u7406\u5e7f\u6cdb\u7684\u6570\u5b57\u7c7b\u578b\u3002\u7136\u800c\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7684\u6267\u884c\u901f\u5ea6\u53ef\u80fd\u4f1a\u6bd4<code>str.isdigit()<\/code>\u7a0d\u6162\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u4ee3\u7801\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def is_number(s):<\/p>\n<p>    try:<\/p>\n<p>        float(s)<\/p>\n<p>        return True<\/p>\n<p>    except ValueError:<\/p>\n<p>        return False<\/p>\n<p>input_value = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u503c\uff1a&quot;)<\/p>\n<p>if is_number(input_value):<\/p>\n<p>    print(f&quot;{input_value} \u662f\u4e00\u4e2a\u6570\u5b57\u3002&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{input_value} \u4e0d\u662f\u4e00\u4e2a\u6570\u5b57\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u4ecb\u7ecd<\/h4>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u7684\u6a21\u5f0f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5224\u65ad\u8f93\u5165\u662f\u5426\u4e3a\u6570\u5b57\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f18\u70b9\u548c\u5c40\u9650\u6027<\/h4>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u65b9\u6cd5\u975e\u5e38\u7075\u6d3b\uff0c\u80fd\u591f\u5904\u7406\u5404\u79cd\u590d\u6742\u7684\u6a21\u5f0f\u3002\u7136\u800c\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u8bed\u6cd5\u8f83\u4e3a\u590d\u6742\uff0c\u9700\u8981\u4e00\u4e9b\u5b66\u4e60\u6210\u672c\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u4ee3\u7801\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def is_number(s):<\/p>\n<p>    pattern = re.compile(r&#39;^-?\\d+(\\.\\d+)?$&#39;)<\/p>\n<p>    return bool(pattern.match(s))<\/p>\n<p>input_value = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u503c\uff1a&quot;)<\/p>\n<p>if is_number(input_value):<\/p>\n<p>    print(f&quot;{input_value} \u662f\u4e00\u4e2a\u6570\u5b57\u3002&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{input_value} \u4e0d\u662f\u4e00\u4e2a\u6570\u5b57\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<code>r&#39;^-?\\d+(\\.\\d+)?$&#39;<\/code>\u7528\u4e8e\u5339\u914d\u6574\u6570\u548c\u6d6e\u70b9\u6570\u3002\u5b83\u8868\u793a\u4e00\u4e2a\u53ef\u9009\u7684\u8d1f\u53f7\u540e\u8ddf\u4e00\u4e2a\u6216\u591a\u4e2a\u6570\u5b57\u5b57\u7b26\uff0c\u63a5\u7740\u662f\u4e00\u4e2a\u53ef\u9009\u7684\u5c0f\u6570\u70b9\u548c\u4e00\u4e2a\u6216\u591a\u4e2a\u6570\u5b57\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u4ecb\u7ecd<\/h4>\n<\/p>\n<p><p>Python\u5185\u7f6e\u51fd\u6570\u5982<code>int()<\/code>\u548c<code>float()<\/code>\u53ef\u4ee5\u7528\u4e8e\u5224\u65ad\u8f93\u5165\u662f\u5426\u4e3a\u6570\u5b57\u7c7b\u578b\u3002\u6211\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u5c06\u8f93\u5165\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570\uff0c\u5e76\u6355\u83b7\u53ef\u80fd\u7684\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f18\u70b9\u548c\u5c40\u9650\u6027<\/h4>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u5b83\u5229\u7528\u4e86Python\u5185\u7f6e\u51fd\u6570\uff0c\u4ee3\u7801\u7b80\u5355\u660e\u4e86\u3002\u7136\u800c\uff0c\u5b83\u53ea\u80fd\u5224\u65ad\u6574\u6570\u6216\u6d6e\u70b9\u6570\uff0c\u4e0d\u80fd\u5224\u65ad\u66f4\u590d\u6742\u7684\u6570\u5b57\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u4ee3\u7801\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def is_int(s):<\/p>\n<p>    try:<\/p>\n<p>        int(s)<\/p>\n<p>        return True<\/p>\n<p>    except ValueError:<\/p>\n<p>        return False<\/p>\n<p>def is_float(s):<\/p>\n<p>    try:<\/p>\n<p>        float(s)<\/p>\n<p>        return True<\/p>\n<p>    except ValueError:<\/p>\n<p>        return False<\/p>\n<p>input_value = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u503c\uff1a&quot;)<\/p>\n<p>if is_int(input_value):<\/p>\n<p>    print(f&quot;{input_value} \u662f\u4e00\u4e2a\u6574\u6570\u3002&quot;)<\/p>\n<p>elif is_float(input_value):<\/p>\n<p>    print(f&quot;{input_value} \u662f\u4e00\u4e2a\u6d6e\u70b9\u6570\u3002&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{input_value} \u4e0d\u662f\u4e00\u4e2a\u6570\u5b57\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u51fd\u6570<code>is_int<\/code>\u548c<code>is_float<\/code>\u5206\u522b\u7528\u4e8e\u5224\u65ad\u8f93\u5165\u662f\u5426\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570\u3002\u6839\u636e\u8f93\u5165\u7684\u7c7b\u578b\uff0c\u7a0b\u5e8f\u4f1a\u8f93\u51fa\u76f8\u5e94\u7684\u63d0\u793a\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u5224\u65adPython\u4e2d\u7684\u8f93\u5165\u662f\u5426\u4e3a\u6570\u5b57\u7c7b\u578b\u65f6\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4f9b\u9009\u62e9\u3002<strong>str.isdigit()\u65b9\u6cd5\u7b80\u5355\u6613\u7528\uff0c\u4f46\u53ea\u80fd\u5224\u65ad\u6574\u6570\uff1btry-except\u6355\u83b7\u5f02\u5e38\u7684\u65b9\u6cd5\u7075\u6d3b\u4e14\u80fd\u591f\u5904\u7406\u5404\u79cd\u6570\u5b57\u7c7b\u578b\uff0c\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff1b\u6b63\u5219\u8868\u8fbe\u5f0f\u65b9\u6cd5\u5f3a\u5927\u4f46\u8bed\u6cd5\u590d\u6742\uff1b\u5229\u7528\u5185\u7f6e\u51fd\u6570\u7684\u65b9\u6cd5\u7b80\u5355\u660e\u4e86\uff0c\u4f46\u53ea\u80fd\u5224\u65ad\u6574\u6570\u6216\u6d6e\u70b9\u6570\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e0d\u540c\u7684\u65b9\u6cd5\u6709\u5404\u81ea\u7684\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0ctry-except\u6355\u83b7\u5f02\u5e38\u7684\u65b9\u6cd5\u901a\u5e38\u662f\u6700\u63a8\u8350\u7684\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u5904\u7406\u5e7f\u6cdb\u7684\u6570\u5b57\u7c7b\u578b\uff0c\u4ee3\u7801\u7b80\u6d01\u4e14\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0c\u7406\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u5728Python\u7f16\u7a0b\u4e2d\u66f4\u597d\u5730\u5904\u7406\u8f93\u5165\u9a8c\u8bc1\u548c\u6570\u636e\u5904\u7406\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u9760\u6027\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\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>isinstance()<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u8f93\u5165\u662f\u5426\u4e3a\u6570\u5b57\u7c7b\u578b\u3002\u901a\u8fc7\u5c06\u8f93\u5165\u8f6c\u6362\u4e3a\u76f8\u5e94\u7684\u6570\u636e\u7c7b\u578b\uff08\u5982<code>int<\/code>\u6216<code>float<\/code>\uff09\uff0c\u5e76\u4f7f\u7528<code>try-except<\/code>\u5757\u6355\u6349\u53ef\u80fd\u7684\u5f02\u5e38\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff1a&quot;)\ntry:\n    number = float(user_input)  # \u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\n    print(&quot;\u8f93\u5165\u662f\u6570\u5b57\u7c7b\u578b&quot;)\nexcept ValueError:\n    print(&quot;\u8f93\u5165\u4e0d\u662f\u6570\u5b57\u7c7b\u578b&quot;)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u7528\u6237\u8f93\u5165\u4e2d\u7684\u975e\u6570\u5b57\u5b57\u7b26\uff1f<\/strong><br \/>\u7528\u6237\u8f93\u5165\u53ef\u80fd\u5305\u542b\u5b57\u6bcd\u6216\u7b26\u53f7\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u68c0\u6d4b\u8fd9\u4e9b\u975e\u6570\u5b57\u5b57\u7b26\u3002\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u4e2d\u7684<code>match()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9a8c\u8bc1\u8f93\u5165\u662f\u5426\u5b8c\u5168\u7531\u6570\u5b57\u7ec4\u6210\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import re\n\nuser_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff1a&quot;)\nif re.match(r&#39;^-?\\d+(\\.\\d+)?$&#39;, user_input):\n    print(&quot;\u8f93\u5165\u662f\u6570\u5b57\u7c7b\u578b&quot;)\nelse:\n    print(&quot;\u8f93\u5165\u4e0d\u662f\u6570\u5b57\u7c7b\u578b&quot;)\n<\/code><\/pre>\n<p><strong>\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u8f93\u5165\u7684\u503c\u5728\u7279\u5b9a\u8303\u56f4\u5185\uff1f<\/strong><br \/>\u9664\u4e86\u9a8c\u8bc1\u8f93\u5165\u662f\u5426\u4e3a\u6570\u5b57\u7c7b\u578b\uff0c\u60a8\u8fd8\u53ef\u4ee5\u8bbe\u7f6e\u6761\u4ef6\u6765\u68c0\u67e5\u8f93\u5165\u7684\u503c\u662f\u5426\u5728\u7279\u5b9a\u8303\u56f4\u5185\u3002\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\u7c7b\u578b\u68c0\u67e5\u548c\u8303\u56f4\u9a8c\u8bc1\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff080-100\uff09\uff1a&quot;)\ntry:\n    number = float(user_input)\n    if 0 &lt;= number &lt;= 100:\n        print(&quot;\u8f93\u5165\u5728\u8303\u56f4\u5185&quot;)\n    else:\n        print(&quot;\u8f93\u5165\u4e0d\u5728\u8303\u56f4\u5185&quot;)\nexcept ValueError:\n    print(&quot;\u8f93\u5165\u4e0d\u662f\u6570\u5b57\u7c7b\u578b&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u60a8\u6709\u6548\u5730\u5904\u7406\u548c\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5224\u65adPython\u4e2d\u7684\u8f93\u5165\u662f\u5426\u4e3a\u6570\u5b57\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1astr.isdigit()\u3001try-except\u6355 [&hellip;]","protected":false},"author":3,"featured_media":1094846,"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\/1094842"}],"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=1094842"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094842\/revisions"}],"predecessor-version":[{"id":1094847,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094842\/revisions\/1094847"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1094846"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1094842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1094842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1094842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}