{"id":266383,"date":"2024-05-15T16:44:18","date_gmt":"2024-05-15T08:44:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/266383.html"},"modified":"2024-05-15T16:44:23","modified_gmt":"2024-05-15T08:44:23","slug":"%e6%80%8e%e4%b9%88%e7%94%a8-python-%e4%bb%a3%e7%a0%81%e5%ae%9e%e7%8e%b0%e4%b8%80%e4%b8%aa%e7%ae%80%e5%8d%95%e7%9a%84%e5%87%af%e6%92%92%e5%8a%a0%e5%af%86%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/266383.html","title":{"rendered":"\u600e\u4e48\u7528 Python \u4ee3\u7801\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27131229\/614b9ce0-9eed-41ac-a96e-5bf7d5319125.webp\" alt=\"\u600e\u4e48\u7528 Python \u4ee3\u7801\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\" \/><\/p>\n<p><p>\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\u662f\u4e00\u79cd\u53e4\u8001\u800c\u7b80\u5355\u7684\u52a0\u5bc6\u6280\u672f\uff0c<strong>\u901a\u8fc7\u5bf9\u5b57\u6bcd\u5728\u5b57\u6bcd\u8868\u4e2d\u7684\u4f4d\u7f6e\u8fdb\u884c\u504f\u79fb\u6765\u52a0\u5bc6\u4fe1\u606f\u3002\u5173\u952e\u6b65\u9aa4\u5305\u62ec\u786e\u5b9a\u504f\u79fb\u91cf\u3001\u5904\u7406\u5b57\u6bcd\u504f\u79fb\u4ee5\u53ca\u5904\u7406\u975e\u5b57\u6bcd\u5b57\u7b26\u3002<\/strong> \u5728Python\u4e2d\u5b9e\u73b0\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u953b\u70bc\u7f16\u7a0b\u80fd\u529b\uff0c\u8fd8\u80fd\u52a0\u6df1\u5bf9\u52a0\u5bc6\u6280\u672f\u7684\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\u6982\u8ff0<\/strong><\/p>\n<\/p>\n<p><p>\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\u7684\u4e3b\u8981\u601d\u60f3\u662f\u5bf9\u5b57\u6bcd\u6309\u7167\u4e00\u5b9a\u7684\u504f\u79fb\u91cf\u8fdb\u884c\u5e73\u79fb\u3002\u6bd4\u5982\uff0c\u504f\u79fb\u91cf\u4e3a3\u65f6\uff0c&#039;A&#039;\u4f1a\u88ab\u52a0\u5bc6\u6210&#039;D&#039;\uff0c&#039;B&#039;\u53d8\u6210&#039;E&#039;\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002\u5230\u8fbe\u5b57\u6bcd\u8868\u672b\u5c3e\u540e\uff0c\u518d\u4ece\u5934\u5f00\u59cb\u8ba1\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e00\u8fc7\u7a0b\uff0c\u6211\u4eec\u9700\u8981\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u504f\u79fb\u91cf\uff0c\u7136\u540e\u5c06\u6bcf\u4e2a\u5b57\u6bcd\u6839\u636e\u8fd9\u4e2a\u504f\u79fb\u91cf\u5e73\u79fb\u3002\u9488\u5bf9\u82f1\u6587\u6765\u8bf4\uff0c\u9700\u8981\u5206\u522b\u5904\u7406\u5c0f\u5199\u548c\u5927\u5199\u5b57\u6bcd\uff0c\u786e\u4fdd&#039;A&#039;\u52a0\u5bc6\u540e\u4ecd\u662f\u5927\u5199\u5b57\u6bcd\uff0c&#039;a&#039;\u52a0\u5bc6\u540e\u4ecd\u662f\u5c0f\u5199\u5b57\u6bcd\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u786e\u5b9a\u504f\u79fb\u91cf<\/strong><\/p>\n<\/p>\n<p><p>\u786e\u5b9a\u504f\u79fb\u91cf\u662f\u5b9e\u73b0\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\u7684\u7b2c\u4e00\u6b65\u3002\u504f\u79fb\u91cf\u5b9a\u4e49\u4e86\u6bcf\u4e2a\u5b57\u6bcd\u5728\u52a0\u5bc6\u8fc7\u7a0b\u4e2d\u9700\u8981\u79fb\u52a8\u7684\u4f4d\u7f6e\u6570\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u53d8\u91cf\u6765\u786e\u5b9a\u504f\u79fb\u91cf\u3002\u4f8b\u5982\uff0c\u504f\u79fb\u91cf\u4e3a3\u610f\u5473\u7740\u6bcf\u4e2a\u5b57\u6bcd\u90fd\u5c06\u5411\u540e\u79fb\u52a8\u4e09\u4e2a\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">shift = 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u504f\u79fb\u91cf\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8bbe\u5b9a\u4e3a\u4efb\u4f55\u503c\uff0c\u4f46\u4e3a\u4fdd\u6301\u52a0\u5bc6\u8fc7\u7a0b\u7684\u7b80\u5355\u6027\uff0c\u4e00\u822c\u53d6\u503c\u4e3a1\u81f325\u4e4b\u95f4\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e09\u3001\u5904\u7406\u5b57\u6bcd\u504f\u79fb<\/strong><\/p>\n<\/p>\n<p><p>\u5904\u7406\u5b57\u6bcd\u504f\u79fb\u662f\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\u7684\u6838\u5fc3\u6b65\u9aa4\u3002\u6211\u4eec\u9700\u8981\u5c06\u6587\u672c\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u6839\u636e\u504f\u79fb\u91cf\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<ol>\n<li>\u9996\u5148\uff0c\u9700\u8981\u68c0\u67e5\u5f53\u524d\u5b57\u7b26\u662f\u5426\u4e3a\u5b57\u6bcd\u3002\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528<code>isalpha()<\/code>\u65b9\u6cd5\u8fdb\u884c\u5224\u65ad\u3002<\/li>\n<li>\u63a5\u4e0b\u6765\uff0c\u6839\u636e\u5b57\u7b26\u662f\u5927\u5199\u8fd8\u662f\u5c0f\u5199\uff0c\u4f7f\u7528\u4e0d\u540c\u7684ASCII\u7801\u8303\u56f4\u8fdb\u884c\u5904\u7406\u3002\u5927\u5199\u5b57\u6bcd\u7684ASCII\u7801\u8303\u56f4\u662f65\u81f390\uff0c\u5c0f\u5199\u5b57\u6bcd\u7684ASCII\u7801\u8303\u56f4\u662f97\u81f3122\u3002<\/li>\n<li>\u5bf9\u4e8e\u6bcf\u4e2a\u5b57\u6bcd\uff0c\u6211\u4eec\u6839\u636e\u504f\u79fb\u91cf\u8ba1\u7b97\u65b0\u7684ASCII\u7801\u503c\u3002\u5982\u679c\u65b0\u7684ASCII\u7801\u8d85\u51fa\u4e86\u5b57\u6bcd\u7684ASCII\u7801\u8303\u56f4\uff0c\u5219\u9700\u8981\u8fdb\u884c\u9002\u5f53\u7684\u8c03\u6574\uff0c\u4ee5\u786e\u4fdd\u7ed3\u679c\u4ecd\u7136\u662f\u4e00\u4e2a\u5b57\u6bcd\u3002<\/li>\n<li>\u6700\u540e\uff0c\u5c06\u65b0\u7684ASCII\u7801\u8f6c\u6362\u56de\u5b57\u7b26\uff0c\u62fc\u63a5\u5230\u7ed3\u679c\u5b57\u7b26\u4e32\u4e2d\u3002<\/li>\n<\/ol>\n<p><p>\u8fd9\u4e00\u8fc7\u7a0b\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684Python\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def caesar_cipher(text, shift):<\/p>\n<p>    result = &quot;&quot;<\/p>\n<p>    for char in text:<\/p>\n<p>        if char.isalpha():<\/p>\n<p>            start = 65 if char.isupper() else 97<\/p>\n<p>            result += chr((ord(char) + shift - start) % 26 + start)<\/p>\n<p>        else:<\/p>\n<p>            result += char<\/p>\n<p>    return result<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>ord()<\/code>\u51fd\u6570\u7528\u4e8e\u83b7\u53d6\u5b57\u7b26\u7684ASCII\u7801\u503c\uff0c<code>chr()<\/code>\u51fd\u6570\u7528\u4e8e\u5c06ASCII\u7801\u503c\u8f6c\u6362\u56de\u5b57\u7b26\u3002<code>% 26<\/code>\u786e\u4fdd\u7ed3\u679c\u4ecd\u7136\u662f\u4e00\u4e2a\u5b57\u6bcd\u3002<\/p>\n<\/p>\n<p><p><strong>\u56db\u3001\u5904\u7406\u975e\u5b57\u6bcd\u5b57\u7b26<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u771f\u5b9e\u7684\u6587\u672c\u4e2d\uff0c\u9664\u4e86\u5b57\u6bcd\u4e4b\u5916\uff0c\u8fd8\u4f1a\u5b58\u5728\u8bb8\u591a\u975e\u5b57\u6bcd\u5b57\u7b26\uff0c\u5982\u6570\u5b57\u3001\u6807\u70b9\u7b26\u53f7\u7b49\u3002\u5728\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\u4e2d\uff0c\u8fd9\u4e9b\u975e\u5b57\u6bcd\u5b57\u7b26\u4e0d\u4f1a\u88ab\u52a0\u5bc6\uff0c\u5e94\u4fdd\u6301\u539f\u6837\u3002<\/p>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u7684<code>caesar_cipher<\/code>\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5224\u65ad<code>if char.isalpha():<\/code>\u6765\u533a\u5206\u5b57\u7b26\u662f\u5426\u4e3a\u5b57\u6bcd\u3002\u5bf9\u4e8e\u975e\u5b57\u6bcd\u5b57\u7b26\uff0c\u76f4\u63a5\u5c06\u5176\u62fc\u63a5\u5230\u7ed3\u679c\u5b57\u7b26\u4e32\u4e2d\uff0c\u4e0d\u505a\u4efb\u4f55\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e94\u3001\u5b8c\u6574\u7684Python\u5b9e\u73b0<\/strong><\/p>\n<\/p>\n<p><p>\u5c06\u4e0a\u8ff0\u6b65\u9aa4\u7efc\u5408\u8d77\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u5f97\u5b8c\u6574\u7684\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\u7684Python\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def caesar_cipher(text, shift):<\/p>\n<p>    result = &quot;&quot;<\/p>\n<p>    for char in text:<\/p>\n<p>        if char.isalpha():<\/p>\n<p>            start = 65 if char.isupper() else 97<\/p>\n<p>            result += chr((ord(char) + shift - start) % 26 + start)<\/p>\n<p>        else:<\/p>\n<p>            result += char<\/p>\n<p>    return result<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>text = &quot;Hello, World!&quot;<\/p>\n<p>shift = 3<\/p>\n<p>encrypted = caesar_cipher(text, shift)<\/p>\n<p>print(&quot;Original: &quot;, text)<\/p>\n<p>print(&quot;Encrypted: &quot;, encrypted)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u9996\u5148\u5b9a\u4e49\u4e86<code>caesar_cipher<\/code>\u51fd\u6570\uff0c\u63a5\u53d7\u6587\u672c\u548c\u504f\u79fb\u91cf\u4f5c\u4e3a\u53c2\u6570\u3002\u5b83\u904d\u5386\u6587\u672c\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5bf9\u5b57\u6bcd\u8fdb\u884c\u52a0\u5bc6\u5904\u7406\uff0c\u5bf9\u975e\u5b57\u6bcd\u5b57\u7b26\u4fdd\u6301\u4e0d\u53d8\u3002\u6700\u540e\u8f93\u51fa\u52a0\u5bc6\u540e\u7684\u6587\u672c\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8c03\u6574<code>shift<\/code>\u503c\uff0c\u4f60\u53ef\u4ee5\u5bf9\u6587\u672c\u8fdb\u884c\u4e0d\u540c\u7a0b\u5ea6\u7684\u52a0\u5bc6\u3002\u8fd9\u79cd\u52a0\u5bc6\u65b9\u5f0f\u867d\u7136\u7b80\u5355\uff0c\u4f46\u5bf9\u4e8e\u521d\u5b66\u8005\u6765\u8bf4\uff0c\u662f\u7406\u89e3\u57fa\u672c\u52a0\u5bc6\u6982\u5ff5\u548c\u7ec3\u4e60\u7f16\u7a0b\u6280\u80fd\u7684\u597d\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u4ec0\u4e48\u662f\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\uff1f<\/strong><\/p>\n<p>\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\u662f\u4e00\u79cd\u7b80\u5355\u7684\u66ff\u6362\u52a0\u5bc6\u65b9\u6cd5\uff0c\u5b83\u5c06\u660e\u6587\u4e2d\u7684\u6bcf\u4e2a\u5b57\u6bcd\u6309\u7167\u6307\u5b9a\u7684\u504f\u79fb\u91cf\u8fdb\u884c\u66ff\u6362\uff0c\u4ece\u800c\u5f97\u5230\u5bc6\u6587\u3002\u504f\u79fb\u91cf\u53ef\u4ee5\u662f\u56fa\u5b9a\u7684\uff0c\u4e5f\u53ef\u4ee5\u6839\u636e\u7528\u6237\u7684\u8f93\u5165\u8fdb\u884c\u53d8\u5316\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u7528 Python \u4ee3\u7801\u5b9e\u73b0\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\uff1f<\/strong><\/p>\n<p>\u5728 Python \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684 ASCII \u7801\u503c\u6765\u5b9e\u73b0\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\u3002\u9996\u5148\uff0c\u5c06\u6bcf\u4e2a\u5b57\u6bcd\u5411\u540e\u79fb\u52a8\u6307\u5b9a\u7684\u504f\u79fb\u91cf\uff0c\u7136\u540e\u5c06\u79fb\u52a8\u540e\u7684\u5b57\u7b26\u518d\u8f6c\u6362\u56de\u5b57\u6bcd\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684 Python \u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">def caesar_encrypt(text, shift):\n    encrypted_text = &quot;&quot;\n    for char in text:\n        if char.isalpha():\n            ascii_start = ord(&#039;a&#039;) if char.islower() else ord(&#039;A&#039;)\n            encrypted_char = chr((ord(char) - ascii_start + shift) % 26 + ascii_start)\n            encrypted_text += encrypted_char\n        else:\n            encrypted_text += char\n    return encrypted_text\n\npl<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ntext = input(&quot;\u8bf7\u8f93\u5165\u660e\u6587\uff1a&quot;)\nshift = int(input(&quot;\u8bf7\u8f93\u5165\u504f\u79fb\u91cf\uff1a&quot;))\nciphertext = caesar_encrypt(plaintext, shift)\nprint(&quot;\u5bc6\u6587\uff1a&quot;, ciphertext)\n<\/code><\/pre>\n<p><strong>3. \u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\u6709\u4ec0\u4e48\u5c40\u9650\u6027\uff1f<\/strong><\/p>\n<p>\u5c3d\u7ba1\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\u7b80\u5355\u4e14\u6613\u4e8e\u5b9e\u73b0\uff0c\u4f46\u5b83\u4e5f\u6709\u4e00\u4e9b\u660e\u663e\u7684\u5c40\u9650\u6027\u3002\u9996\u5148\uff0c\u5b83\u53ef\u4ee5\u88ab\u8f7b\u677e\u7834\u89e3\uff0c\u56e0\u4e3a\u53ea\u670926\u79cd\u53ef\u80fd\u7684\u504f\u79fb\u91cf\u3002\u5176\u6b21\uff0c\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\u53ea\u80fd\u52a0\u5bc6\u5b57\u6bcd\uff0c\u800c\u65e0\u6cd5\u52a0\u5bc6\u5176\u4ed6\u5b57\u7b26\u3002\u6700\u540e\uff0c\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\u4e5f\u5bb9\u6613\u53d7\u5230\u9891\u7387\u5206\u6790\u7684\u653b\u51fb\uff0c\u56e0\u4e3a\u5b57\u6bcd\u7684\u51fa\u73b0\u9891\u7387\u5728\u52a0\u5bc6\u540e\u4ecd\u7136\u4fdd\u6301\u4e0d\u53d8\u3002\u56e0\u6b64\uff0c\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\u901a\u5e38\u88ab\u89c6\u4e3a\u4e00\u79cd\u57fa\u7840\u7684\u52a0\u5bc6\u65b9\u6cd5\uff0c\u800c\u4e0d\u9002\u7528\u4e8e\u5bf9\u5b89\u5168\u6027\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u51ef\u6492\u52a0\u5bc6\u7b97\u6cd5\u662f\u4e00\u79cd\u53e4\u8001\u800c\u7b80\u5355\u7684\u52a0\u5bc6\u6280\u672f\uff0c\u901a\u8fc7\u5bf9\u5b57\u6bcd\u5728\u5b57\u6bcd\u8868\u4e2d\u7684\u4f4d\u7f6e\u8fdb\u884c\u504f\u79fb\u6765\u52a0\u5bc6\u4fe1\u606f\u3002\u5173\u952e\u6b65\u9aa4\u5305\u62ec\u786e\u5b9a\u504f\u79fb\u91cf\u3001 [&hellip;]","protected":false},"author":3,"featured_media":266388,"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\/266383"}],"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=266383"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/266383\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/266388"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=266383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=266383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=266383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}