{"id":1180352,"date":"2025-01-15T18:35:42","date_gmt":"2025-01-15T10:35:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1180352.html"},"modified":"2025-01-15T18:35:45","modified_gmt":"2025-01-15T10:35:45","slug":"python%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e8%be%93%e5%85%a5%e5%86%85%e5%ae%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1180352.html","title":{"rendered":"Python\u5982\u4f55\u5224\u65ad\u8f93\u5165\u5185\u5bb9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25114516\/af0de98e-784a-4ca8-83ee-51b3ae240def.webp\" alt=\"Python\u5982\u4f55\u5224\u65ad\u8f93\u5165\u5185\u5bb9\" \/><\/p>\n<p><p> <strong>Python\u5224\u65ad\u8f93\u5165\u5185\u5bb9\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u7c7b\u578b\u8f6c\u6362\u3001\u5f02\u5e38\u5904\u7406\u6765\u5b9e\u73b0\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u6700\u5e38\u7528\u4e14\u5f3a\u5927\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u8ba9\u6211\u4eec\u5bf9\u8f93\u5165\u5185\u5bb9\u8fdb\u884c\u590d\u6742\u6a21\u5f0f\u7684\u5339\u914d\u548c\u9a8c\u8bc1\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u9a8c\u8bc1\u4e00\u4e2a\u8f93\u5165\u662f\u5426\u662f\u6709\u6548\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8be6\u7ec6\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u9700\u8981\u5224\u65ad\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u662f\u5426\u4e3a\u6709\u6548\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u3002\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u7684\u683c\u5f0f\u4e3a\uff1a<a href=\"mailto:username@domain.extension\">username@dom<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.extension<\/a>\u3002\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u8fd9\u79cd\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def is_valid_email(email):<\/p>\n<p>    # \u5b9a\u4e49\u5339\u914d\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<\/p>\n<p>    regex = r&#39;^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$&#39;<\/p>\n<p>    if re.match(regex, email):<\/p>\n<p>        return True<\/p>\n<p>    else:<\/p>\n<p>        return False<\/p>\n<p>email = input(&quot;\u8bf7\u8f93\u5165\u7535\u5b50\u90ae\u4ef6\u5730\u5740: &quot;)<\/p>\n<p>if is_valid_email(email):<\/p>\n<p>    print(&quot;\u8fd9\u662f\u4e00\u4e2a\u6709\u6548\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u8fd9\u4e0d\u662f\u4e00\u4e2a\u6709\u6548\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>re<\/code> \u6a21\u5757\u4e2d\u7684 <code>re.match<\/code> \u51fd\u6570\u6765\u5339\u914d\u8f93\u5165\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u662f\u5426\u7b26\u5408\u9884\u5b9a\u4e49\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u3002\u5982\u679c\u5339\u914d\u6210\u529f\uff0c\u5219\u8f93\u5165\u7684\u662f\u4e00\u4e2a\u6709\u6548\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff0c\u5426\u5219\u4e0d\u662f\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u4ece\u591a\u4e2a\u65b9\u9762\u8be6\u7ec6\u4ecb\u7ecdPython\u5982\u4f55\u5224\u65ad\u8f93\u5165\u5185\u5bb9\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u5185\u7f6e\u51fd\u6570\u5224\u65ad<\/strong><\/h2>\n<p><p>Python \u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u5224\u65ad\u8f93\u5165\u5185\u5bb9\u7684\u7c7b\u578b\u548c\u7279\u6027\u3002<\/p>\n<\/p>\n<p><h2>1.1\u3001\u4f7f\u7528 <code>isnumeric<\/code> \u548c <code>isdigit<\/code><\/h2>\n<\/p>\n<p><p>\u5f53\u6211\u4eec\u9700\u8981\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u6570\u5b57\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>isnumeric<\/code> \u548c <code>isdigit<\/code> \u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5c06\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u8868\u793a\u5b57\u7b26\u4e32\u662f\u5426\u5b8c\u5168\u7531\u6570\u5b57\u5b57\u7b26\u7ec4\u6210\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57: &quot;)<\/p>\n<p>if num.isnumeric():<\/p>\n<p>    print(f&quot;{num} \u662f\u4e00\u4e2a\u6570\u5b57&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{num} \u4e0d\u662f\u4e00\u4e2a\u6570\u5b57&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>1.2\u3001\u4f7f\u7528 <code>isalpha<\/code> \u548c <code>isalnum<\/code><\/h2>\n<\/p>\n<p><p>\u5f53\u6211\u4eec\u9700\u8981\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u4ec5\u5305\u542b\u5b57\u6bcd\u6216\u5b57\u6bcd\u548c\u6570\u5b57\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>isalpha<\/code> \u548c <code>isalnum<\/code> \u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = input(&quot;\u8bf7\u8f93\u5165\u4e00\u6bb5\u6587\u672c: &quot;)<\/p>\n<p>if text.isalpha():<\/p>\n<p>    print(f&quot;{text} \u4ec5\u5305\u542b\u5b57\u6bcd&quot;)<\/p>\n<p>elif text.isalnum():<\/p>\n<p>    print(f&quot;{text} \u5305\u542b\u5b57\u6bcd\u548c\u6570\u5b57&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{text} \u5305\u542b\u5176\u4ed6\u5b57\u7b26&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u7c7b\u578b\u8f6c\u6362\u5224\u65ad<\/strong><\/h2>\n<p><p>\u901a\u8fc7\u7c7b\u578b\u8f6c\u6362\uff0c\u6211\u4eec\u53ef\u4ee5\u5224\u65ad\u8f93\u5165\u5185\u5bb9\u662f\u5426\u53ef\u4ee5\u8f6c\u6362\u4e3a\u7279\u5b9a\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u5c06\u8f93\u5165\u5185\u5bb9\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570\uff0c\u5e76\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6765\u6355\u83b7\u8f6c\u6362\u5931\u8d25\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h2>2.1\u3001\u6574\u6570\u8f6c\u6362<\/h2>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>int()<\/code> \u51fd\u6570\u5c1d\u8bd5\u5c06\u8f93\u5165\u5185\u5bb9\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u5982\u679c\u8f6c\u6362\u5931\u8d25\uff0c\u5c06\u5f15\u53d1 <code>ValueError<\/code> \u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570: &quot;)<\/p>\n<p>try:<\/p>\n<p>    num = int(num)<\/p>\n<p>    print(f&quot;{num} \u662f\u4e00\u4e2a\u6574\u6570&quot;)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(f&quot;{num} \u4e0d\u662f\u4e00\u4e2a\u6574\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2.2\u3001\u6d6e\u70b9\u6570\u8f6c\u6362<\/h2>\n<\/p>\n<p><p>\u7c7b\u4f3c\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>float()<\/code> \u51fd\u6570\u5c1d\u8bd5\u5c06\u8f93\u5165\u5185\u5bb9\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6d6e\u70b9\u6570: &quot;)<\/p>\n<p>try:<\/p>\n<p>    num = float(num)<\/p>\n<p>    print(f&quot;{num} \u662f\u4e00\u4e2a\u6d6e\u70b9\u6570&quot;)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(f&quot;{num} \u4e0d\u662f\u4e00\u4e2a\u6d6e\u70b9\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u5224\u65ad<\/strong><\/h2>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5339\u914d\u590d\u6742\u7684\u6a21\u5f0f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>re<\/code> \u6a21\u5757\u4e2d\u7684\u51fd\u6570\u6765\u5224\u65ad\u8f93\u5165\u5185\u5bb9\u662f\u5426\u7b26\u5408\u7279\u5b9a\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><h2>3.1\u3001\u5339\u914d\u7535\u5b50\u90ae\u4ef6\u5730\u5740<\/h2>\n<\/p>\n<p><p>\u524d\u9762\u5df2\u7ecf\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u3002\u8fd9\u91cc\u518d\u7ed9\u51fa\u4e00\u4e2a\u8be6\u7ec6\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def is_valid_email(email):<\/p>\n<p>    regex = r&#39;^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$&#39;<\/p>\n<p>    return re.match(regex, email) is not None<\/p>\n<p>email = input(&quot;\u8bf7\u8f93\u5165\u7535\u5b50\u90ae\u4ef6\u5730\u5740: &quot;)<\/p>\n<p>if is_valid_email(email):<\/p>\n<p>    print(&quot;\u8fd9\u662f\u4e00\u4e2a\u6709\u6548\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u8fd9\u4e0d\u662f\u4e00\u4e2a\u6709\u6548\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3.2\u3001\u5339\u914d\u7535\u8bdd\u53f7\u7801<\/h2>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u7535\u8bdd\u53f7\u7801\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u9700\u8981\u9a8c\u8bc1\u4e00\u4e2a\u7535\u8bdd\u53f7\u7801\u662f\u5426\u4e3a\u4e2d\u56fd\u5927\u9646\u7684\u624b\u673a\u53f7\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def is_valid_phone(phone):<\/p>\n<p>    regex = r&#39;^1[3-9]\\d{9}$&#39;<\/p>\n<p>    return re.match(regex, phone) is not None<\/p>\n<p>phone = input(&quot;\u8bf7\u8f93\u5165\u624b\u673a\u53f7\u7801: &quot;)<\/p>\n<p>if is_valid_phone(phone):<\/p>\n<p>    print(&quot;\u8fd9\u662f\u4e00\u4e2a\u6709\u6548\u7684\u624b\u673a\u53f7\u7801&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u8fd9\u4e0d\u662f\u4e00\u4e2a\u6709\u6548\u7684\u624b\u673a\u53f7\u7801&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u5f02\u5e38\u5904\u7406<\/strong><\/h2>\n<p><p>\u5f02\u5e38\u5904\u7406\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5b83\u6765\u6355\u83b7\u548c\u5904\u7406\u8f93\u5165\u5185\u5bb9\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h2>4.1\u3001\u6355\u83b7\u8f6c\u6362\u5f02\u5e38<\/h2>\n<\/p>\n<p><p>\u524d\u9762\u5df2\u7ecf\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6765\u6355\u83b7\u6574\u6570\u548c\u6d6e\u70b9\u6570\u8f6c\u6362\u7684\u5f02\u5e38\u3002\u8fd9\u91cc\u518d\u7ed9\u51fa\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u4f8b\u5b50\uff0c\u5224\u65ad\u8f93\u5165\u5185\u5bb9\u662f\u5426\u4e3a\u6709\u6548\u7684\u65e5\u671f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>date_str = input(&quot;\u8bf7\u8f93\u5165\u65e5\u671f (YYYY-MM-DD): &quot;)<\/p>\n<p>try:<\/p>\n<p>    date_obj = datetime.strptime(date_str, &#39;%Y-%m-%d&#39;)<\/p>\n<p>    print(f&quot;{date_str} \u662f\u4e00\u4e2a\u6709\u6548\u7684\u65e5\u671f&quot;)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(f&quot;{date_str} \u4e0d\u662f\u4e00\u4e2a\u6709\u6548\u7684\u65e5\u671f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>datetime.strptime<\/code> \u51fd\u6570\u5c1d\u8bd5\u5c06\u8f93\u5165\u5185\u5bb9\u89e3\u6790\u4e3a\u65e5\u671f\u5bf9\u8c61\u3002\u5982\u679c\u89e3\u6790\u5931\u8d25\uff0c\u5c06\u5f15\u53d1 <code>ValueError<\/code> \u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h2>4.2\u3001\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406<\/h2>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684\u5f02\u5e38\u7c7b\uff0c\u5e76\u5728\u8f93\u5165\u9a8c\u8bc1\u4e2d\u4f7f\u7528\u5b83\u4eec\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u9700\u8981\u9a8c\u8bc1\u4e00\u4e2a\u7528\u6237\u540d\u662f\u5426\u7b26\u5408\u7279\u5b9a\u89c4\u5219\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class InvalidUsernameError(Exception):<\/p>\n<p>    pass<\/p>\n<p>def validate_username(username):<\/p>\n<p>    if len(username) &lt; 3:<\/p>\n<p>        raise InvalidUsernameError(&quot;\u7528\u6237\u540d\u957f\u5ea6\u4e0d\u80fd\u5c0f\u4e8e3\u4e2a\u5b57\u7b26&quot;)<\/p>\n<p>    if not username.isalnum():<\/p>\n<p>        raise InvalidUsernameError(&quot;\u7528\u6237\u540d\u53ea\u80fd\u5305\u542b\u5b57\u6bcd\u548c\u6570\u5b57&quot;)<\/p>\n<p>username = input(&quot;\u8bf7\u8f93\u5165\u7528\u6237\u540d: &quot;)<\/p>\n<p>try:<\/p>\n<p>    validate_username(username)<\/p>\n<p>    print(&quot;\u7528\u6237\u540d\u6709\u6548&quot;)<\/p>\n<p>except InvalidUsernameError as e:<\/p>\n<p>    print(f&quot;\u65e0\u6548\u7684\u7528\u6237\u540d: {e}&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 <code>InvalidUsernameError<\/code> \u5f02\u5e38\u7c7b\uff0c\u5e76\u5728 <code>validate_username<\/code> \u51fd\u6570\u4e2d\u4f7f\u7528\u5b83\u6765\u9a8c\u8bc1\u7528\u6237\u540d\u3002\u5982\u679c\u7528\u6237\u540d\u65e0\u6548\uff0c\u5219\u5f15\u53d1\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u5e76\u5728\u4e3b\u7a0b\u5e8f\u4e2d\u6355\u83b7\u548c\u5904\u7406\u8be5\u5f02\u5e38\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u7efc\u5408\u793a\u4f8b<\/strong><\/h2>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5224\u65ad\u8f93\u5165\u5185\u5bb9\uff0c\u6211\u4eec\u7ed3\u5408\u524d\u9762\u7684\u77e5\u8bc6\u70b9\uff0c\u7f16\u5199\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\u3002\u5047\u8bbe\u6211\u4eec\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u6ce8\u518c\u8868\u5355\u9a8c\u8bc1\u7a0b\u5e8f\uff0c\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u7528\u6237\u540d\u3001\u5bc6\u7801\u3001\u7535\u5b50\u90ae\u4ef6\u548c\u624b\u673a\u53f7\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>from datetime import datetime<\/p>\n<p>class ValidationError(Exception):<\/p>\n<p>    pass<\/p>\n<p>def validate_username(username):<\/p>\n<p>    if len(username) &lt; 3:<\/p>\n<p>        raise ValidationError(&quot;\u7528\u6237\u540d\u957f\u5ea6\u4e0d\u80fd\u5c0f\u4e8e3\u4e2a\u5b57\u7b26&quot;)<\/p>\n<p>    if not username.isalnum():<\/p>\n<p>        raise ValidationError(&quot;\u7528\u6237\u540d\u53ea\u80fd\u5305\u542b\u5b57\u6bcd\u548c\u6570\u5b57&quot;)<\/p>\n<p>def validate_password(password):<\/p>\n<p>    if len(password) &lt; 8:<\/p>\n<p>        raise ValidationError(&quot;\u5bc6\u7801\u957f\u5ea6\u4e0d\u80fd\u5c0f\u4e8e8\u4e2a\u5b57\u7b26&quot;)<\/p>\n<p>    if not re.search(r&#39;[A-Z]&#39;, password):<\/p>\n<p>        raise ValidationError(&quot;\u5bc6\u7801\u5fc5\u987b\u5305\u542b\u81f3\u5c11\u4e00\u4e2a\u5927\u5199\u5b57\u6bcd&quot;)<\/p>\n<p>    if not re.search(r&#39;[a-z]&#39;, password):<\/p>\n<p>        raise ValidationError(&quot;\u5bc6\u7801\u5fc5\u987b\u5305\u542b\u81f3\u5c11\u4e00\u4e2a\u5c0f\u5199\u5b57\u6bcd&quot;)<\/p>\n<p>    if not re.search(r&#39;[0-9]&#39;, password):<\/p>\n<p>        raise ValidationError(&quot;\u5bc6\u7801\u5fc5\u987b\u5305\u542b\u81f3\u5c11\u4e00\u4e2a\u6570\u5b57&quot;)<\/p>\n<p>def is_valid_email(email):<\/p>\n<p>    regex = r&#39;^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$&#39;<\/p>\n<p>    return re.match(regex, email) is not None<\/p>\n<p>def is_valid_phone(phone):<\/p>\n<p>    regex = r&#39;^1[3-9]\\d{9}$&#39;<\/p>\n<p>    return re.match(regex, phone) is not None<\/p>\n<p>username = input(&quot;\u8bf7\u8f93\u5165\u7528\u6237\u540d: &quot;)<\/p>\n<p>password = input(&quot;\u8bf7\u8f93\u5165\u5bc6\u7801: &quot;)<\/p>\n<p>email = input(&quot;\u8bf7\u8f93\u5165\u7535\u5b50\u90ae\u4ef6\u5730\u5740: &quot;)<\/p>\n<p>phone = input(&quot;\u8bf7\u8f93\u5165\u624b\u673a\u53f7\u7801: &quot;)<\/p>\n<p>try:<\/p>\n<p>    validate_username(username)<\/p>\n<p>    validate_password(password)<\/p>\n<p>    if not is_valid_email(email):<\/p>\n<p>        raise ValidationError(&quot;\u65e0\u6548\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740&quot;)<\/p>\n<p>    if not is_valid_phone(phone):<\/p>\n<p>        raise ValidationError(&quot;\u65e0\u6548\u7684\u624b\u673a\u53f7\u7801&quot;)<\/p>\n<p>    print(&quot;\u6ce8\u518c\u4fe1\u606f\u6709\u6548&quot;)<\/p>\n<p>except ValidationError as e:<\/p>\n<p>    print(f&quot;\u6ce8\u518c\u4fe1\u606f\u65e0\u6548: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7efc\u5408\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u56db\u4e2a\u9a8c\u8bc1\u51fd\u6570\uff0c\u5206\u522b\u7528\u4e8e\u9a8c\u8bc1\u7528\u6237\u540d\u3001\u5bc6\u7801\u3001\u7535\u5b50\u90ae\u4ef6\u548c\u624b\u673a\u53f7\u7801\u3002\u6211\u4eec\u8fd8\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>ValidationError<\/code> \u5f02\u5e38\u7c7b\uff0c\u7528\u4e8e\u5728\u9a8c\u8bc1\u5931\u8d25\u65f6\u5f15\u53d1\u5f02\u5e38\u3002\u5728\u4e3b\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u6355\u83b7\u5e76\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\uff0c\u8f93\u51fa\u76f8\u5e94\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u7684\u8be6\u7ec6\u4ecb\u7ecd\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u76f8\u4fe1\u60a8\u5df2\u7ecf\u5bf9\u5982\u4f55\u5728Python\u4e2d\u5224\u65ad\u8f93\u5165\u5185\u5bb9\u6709\u4e86\u8f83\u4e3a\u6df1\u5165\u7684\u4e86\u89e3\u3002\u65e0\u8bba\u662f\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u7c7b\u578b\u8f6c\u6362\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u8fd8\u662f\u5f02\u5e38\u5904\u7406\uff0c\u90fd\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u573a\u666f\u7075\u6d3b\u5e94\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u5224\u65ad\u7528\u6237\u8f93\u5165\u7684\u7c7b\u578b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>type()<\/code>\u6765\u68c0\u67e5\u8f93\u5165\u5185\u5bb9\u7684\u7c7b\u578b\u3002\u7528\u6237\u8f93\u5165\u901a\u5e38\u662f\u5b57\u7b26\u4e32\uff0c\u56e0\u6b64\u53ef\u4ee5\u901a\u8fc7\u5c06\u5176\u8f6c\u6362\u4e3a\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\uff08\u5982\u6574\u6570\u3001\u6d6e\u70b9\u6570\u7b49\uff09\u6765\u5224\u65ad\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u5c1d\u8bd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5982\u679c\u8f6c\u6362\u6210\u529f\uff0c\u5219\u8bf4\u660e\u8f93\u5165\u7684\u662f\u4e00\u4e2a\u6570\u5b57\u3002\u4e5f\u53ef\u4ee5\u7ed3\u5408<code>isinstance()<\/code>\u51fd\u6570\u6765\u5224\u65ad\u8f93\u5165\u7684\u7c7b\u578b\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u5f02\u5e38\u60c5\u51b5\uff1f<\/strong><br \/>\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\uff0c\u5e38\u5e38\u4f1a\u9047\u5230\u683c\u5f0f\u4e0d\u6b63\u786e\u6216\u7c7b\u578b\u4e0d\u5339\u914d\u7684\u95ee\u9898\u3002\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u7ed3\u6784\u6765\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\uff0c\u786e\u4fdd\u7a0b\u5e8f\u4e0d\u4f1a\u56e0\u9519\u8bef\u7684\u8f93\u5165\u800c\u5d29\u6e83\u3002\u4f8b\u5982\uff0c\u82e5\u7528\u6237\u8f93\u5165\u4e86\u4e00\u4e2a\u4e0d\u80fd\u8f6c\u6362\u4e3a\u6574\u6570\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u5728<code>except<\/code>\u5757\u4e2d\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\uff0c\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u6709\u6548\u7684\u65b9\u6cd5\u6765\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u6709\u6548\u6027\uff1f<\/strong><br \/>\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u6709\u6548\u6027\u53ef\u4ee5\u901a\u8fc7\u591a\u4e2a\u65b9\u5f0f\u5b9e\u73b0\u3002\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff08<code>re<\/code>\u6a21\u5757\uff09\u6765\u68c0\u67e5\u8f93\u5165\u662f\u5426\u7b26\u5408\u7279\u5b9a\u683c\u5f0f\uff0c\u6bd4\u5982\u90ae\u7bb1\u5730\u5740\u3001\u7535\u8bdd\u53f7\u7801\u7b49\u3002\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u68c0\u67e5\u8f93\u5165\u503c\u662f\u5426\u5728\u67d0\u4e2a\u8303\u56f4\u5185\u6216\u662f\u5426\u4e3a\u9884\u671f\u7684\u9009\u9879\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u786e\u4fdd\u8f93\u5165\u5185\u5bb9\u7684\u51c6\u786e\u6027\u548c\u6709\u6548\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5224\u65ad\u8f93\u5165\u5185\u5bb9\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u7c7b\u578b\u8f6c\u6362\u3001\u5f02\u5e38\u5904\u7406\u6765\u5b9e\u73b0\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u6700\u5e38 [&hellip;]","protected":false},"author":3,"featured_media":1180357,"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\/1180352"}],"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=1180352"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1180352\/revisions"}],"predecessor-version":[{"id":1180358,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1180352\/revisions\/1180358"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1180357"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1180352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1180352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1180352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}