{"id":1182464,"date":"2025-01-15T19:03:29","date_gmt":"2025-01-15T11:03:29","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1182464.html"},"modified":"2025-01-15T19:03:32","modified_gmt":"2025-01-15T11:03:32","slug":"python%e5%a6%82%e4%bd%95%e8%be%93%e5%87%ba%e4%b9%98%e6%b3%95%e5%8f%a3%e8%af%80%e8%a1%a8-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1182464.html","title":{"rendered":"python\u5982\u4f55\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25130429\/ca1724d5-248c-437c-a2f7-de6574641179.webp\" alt=\"python\u5982\u4f55\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868\" \/><\/p>\n<p><p> <strong>Python\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u5faa\u73af\u6765\u5b9e\u73b0\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5bf9\u9f50\u8f93\u51fa\u3001\u4f7f\u7528\u51fd\u6570\u5c01\u88c5\u4ee5\u589e\u5f3a\u4ee3\u7801\u590d\u7528\u6027\u3002<\/strong>\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528Python\u7f16\u5199\u4ee3\u7801\u6765\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868\uff0c\u5e76\u5bf9\u5176\u4e2d\u7684\u5173\u952e\u90e8\u5206\u8fdb\u884c\u8be6\u7ec6\u89e3\u91ca\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u6765\u5b9e\u73b0<\/strong><\/p>\n<\/p>\n<p><p>\u5d4c\u5957\u5faa\u73af\u662f\u5b9e\u73b0\u4e58\u6cd5\u53e3\u8bc0\u8868\u7684\u57fa\u7840\u3002\u5916\u5c42\u5faa\u73af\u63a7\u5236\u884c\u6570\uff0c\u5185\u5c42\u5faa\u73af\u63a7\u5236\u5217\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868<\/p>\n<p>for i in range(1, 10):<\/p>\n<p>    for j in range(1, i + 1):<\/p>\n<p>        print(f&quot;{j}*{i}={i*j}&quot;, end=&quot;\\t&quot;)<\/p>\n<p>    print()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4ee3\u7801\u89e3\u91ca<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u5916\u5c42\u5faa\u73af<\/strong>\uff1a<code>for i in range(1, 10)<\/code>\uff0c\u8868\u793a\u4ece1\u52309\u7684\u884c\u6570\u3002<\/li>\n<li><strong>\u5185\u5c42\u5faa\u73af<\/strong>\uff1a<code>for j in range(1, i + 1)<\/code>\uff0c\u8868\u793a\u6bcf\u4e00\u884c\u4ece1\u5230\u5f53\u524d\u884c\u6570\u7684\u5217\u6570\u3002<\/li>\n<li><strong>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/strong>\uff1a<code>print(f&quot;{j}*{i}={i*j}&quot;, end=&quot;\\t&quot;)<\/code>\uff0c\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u8f93\u51fa\u4e58\u6cd5\u8fd0\u7b97\uff0c\u5e76\u7528\u5236\u8868\u7b26<code>\\t<\/code>\u5bf9\u9f50\u8f93\u51fa\u3002<\/li>\n<li><strong>\u6362\u884c<\/strong>\uff1a\u5185\u5c42\u5faa\u73af\u7ed3\u675f\u540e\uff0c\u4f7f\u7528<code>print()<\/code>\u6765\u6362\u884c\u3002<\/li>\n<\/ul>\n<p><p><strong>\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5bf9\u9f50\u8f93\u51fa<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u4e58\u6cd5\u53e3\u8bc0\u8868\u7684\u8f93\u51fa\u66f4\u52a0\u7f8e\u89c2\uff0c\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6765\u5bf9\u9f50\u6bcf\u4e00\u5217\u7684\u8f93\u51fa\u3002\u4ee5\u4e0b\u662f\u6539\u8fdb\u540e\u7684\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5bf9\u9f50\u8f93\u51fa<\/p>\n<p>for i in range(1, 10):<\/p>\n<p>    for j in range(1, i + 1):<\/p>\n<p>        print(f&quot;{j}*{i}={i*j:2}&quot;, end=&quot; &quot;)<\/p>\n<p>    print()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4ee3\u7801\u89e3\u91ca<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/strong>\uff1a<code>f&quot;{j}*{i}={i*j:2}&quot;<\/code>\uff0c\u5176\u4e2d<code>{i*j:2}<\/code>\u8868\u793a\u4e58\u79ef\u5360\u636e\u4e24\u4f4d\u5bbd\u5ea6\uff0c\u4ece\u800c\u5b9e\u73b0\u5bf9\u9f50\u8f93\u51fa\u3002<\/li>\n<\/ul>\n<p><p><strong>\u4f7f\u7528\u51fd\u6570\u5c01\u88c5\u4ee5\u589e\u5f3a\u4ee3\u7801\u590d\u7528\u6027<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\uff0c\u53ef\u4ee5\u5c06\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868\u7684\u903b\u8f91\u5c01\u88c5\u5230\u4e00\u4e2a\u51fd\u6570\u4e2d\u3002\u8fd9\u6837\uff0c\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u968f\u65f6\u8c03\u7528\u8be5\u51fd\u6570\u6765\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868\u3002\u4ee5\u4e0b\u662f\u51fd\u6570\u5c01\u88c5\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def print_multiplication_table():<\/p>\n<p>    # \u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868<\/p>\n<p>    for i in range(1, 10):<\/p>\n<p>        for j in range(1, i + 1):<\/p>\n<p>            print(f&quot;{j}*{i}={i*j:2}&quot;, end=&quot; &quot;)<\/p>\n<p>        print()<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868<\/strong><\/h2>\n<p>print_multiplication_table()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4ee3\u7801\u89e3\u91ca<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u5b9a\u4e49\u51fd\u6570<\/strong>\uff1a<code>def print_multiplication_table()<\/code>\uff0c\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a<code>print_multiplication_table<\/code>\u7684\u51fd\u6570\u3002<\/li>\n<li><strong>\u51fd\u6570\u4f53<\/strong>\uff1a\u5c06\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868\u7684\u903b\u8f91\u653e\u5165\u51fd\u6570\u4f53\u4e2d\u3002<\/li>\n<li><strong>\u8c03\u7528\u51fd\u6570<\/strong>\uff1a<code>print_multiplication_table()<\/code>\uff0c\u8c03\u7528\u8be5\u51fd\u6570\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868\u3002<\/li>\n<\/ul>\n<p><p><strong>\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5d4c\u5957\u5faa\u73af\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u548c\u51fd\u6570\u5c01\u88c5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0Python\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868\u7684\u529f\u80fd\u3002\u5d4c\u5957\u5faa\u73af\u7528\u4e8e\u904d\u5386\u884c\u548c\u5217\uff0c\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7528\u4e8e\u5bf9\u9f50\u8f93\u51fa\uff0c\u51fd\u6570\u5c01\u88c5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def print_multiplication_table():<\/p>\n<p>    # \u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868<\/p>\n<p>    for i in range(1, 10):<\/p>\n<p>        for j in range(1, i + 1):<\/p>\n<p>            print(f&quot;{j}*{i}={i*j:2}&quot;, end=&quot; &quot;)<\/p>\n<p>        print()<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868<\/strong><\/h2>\n<p>print_multiplication_table()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u5d4c\u5957\u5faa\u73af\u7684\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u662f\u5b9e\u73b0\u4e58\u6cd5\u53e3\u8bc0\u8868\u7684\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u5916\u5c42\u5faa\u73af\u63a7\u5236\u884c\u6570\uff0c\u5185\u5c42\u5faa\u73af\u63a7\u5236\u5217\u6570\u3002\u901a\u8fc7\u5d4c\u5957\u5faa\u73af\uff0c\u53ef\u4ee5\u904d\u5386\u6bcf\u4e00\u884c\u548c\u6bcf\u4e00\u5217\uff0c\u4ece\u800c\u8f93\u51fa\u5b8c\u6574\u7684\u4e58\u6cd5\u53e3\u8bc0\u8868\u3002<\/p>\n<\/p>\n<p><p>\u5d4c\u5957\u5faa\u73af\u7684\u57fa\u672c\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(1, 10):<\/p>\n<p>    for j in range(1, i + 1):<\/p>\n<p>        print(f&quot;{j}*{i}={i*j}&quot;, end=&quot;\\t&quot;)<\/p>\n<p>    print()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7ed3\u6784\u4e2d\uff0c\u5916\u5c42\u5faa\u73af <code>for i in range(1, 10)<\/code> \u8868\u793a\u4ece1\u52309\u7684\u884c\u6570\u3002\u5185\u5c42\u5faa\u73af <code>for j in range(1, i + 1)<\/code> \u8868\u793a\u6bcf\u4e00\u884c\u4ece1\u5230\u5f53\u524d\u884c\u6570\u7684\u5217\u6570\u3002\u5185\u5c42\u5faa\u73af\u4e2d\u7684 <code>print(f&quot;{j}*{i}={i*j}&quot;, end=&quot;\\t&quot;)<\/code> \u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u8f93\u51fa\u4e58\u6cd5\u8fd0\u7b97\uff0c\u5e76\u7528\u5236\u8868\u7b26 <code>\\t<\/code> \u5bf9\u9f50\u8f93\u51fa\u3002\u6bcf\u884c\u8f93\u51fa\u7ed3\u675f\u540e\uff0c\u901a\u8fc7 <code>print()<\/code> \u6362\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5bf9\u9f50\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>\u5728\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868\u65f6\uff0c\u4e3a\u4e86\u4f7f\u8868\u683c\u66f4\u52a0\u6574\u9f50\u7f8e\u89c2\uff0c\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6765\u5bf9\u9f50\u6bcf\u4e00\u5217\u7684\u8f93\u51fa\u3002\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u53ef\u4ee5\u6307\u5b9a\u8f93\u51fa\u7684\u5bbd\u5ea6\u548c\u5bf9\u9f50\u65b9\u5f0f\uff0c\u4ece\u800c\u4f7f\u6bcf\u4e00\u5217\u7684\u5185\u5bb9\u5bf9\u9f50\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5bf9\u9f50\u8f93\u51fa\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(1, 10):<\/p>\n<p>    for j in range(1, i + 1):<\/p>\n<p>        print(f&quot;{j}*{i}={i*j:2}&quot;, end=&quot; &quot;)<\/p>\n<p>    print()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4ee3\u7801\u4e2d\uff0c<code>f&quot;{j}*{i}={i*j:2}&quot;<\/code> \u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d <code>{i*j:2}<\/code> \u8868\u793a\u4e58\u79ef\u5360\u636e\u4e24\u4f4d\u5bbd\u5ea6\uff0c\u4ece\u800c\u5b9e\u73b0\u5bf9\u9f50\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u51fd\u6570\u5c01\u88c5<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\uff0c\u53ef\u4ee5\u5c06\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868\u7684\u903b\u8f91\u5c01\u88c5\u5230\u4e00\u4e2a\u51fd\u6570\u4e2d\u3002\u8fd9\u6837\uff0c\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u968f\u65f6\u8c03\u7528\u8be5\u51fd\u6570\u6765\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868\u3002<\/p>\n<\/p>\n<p><p>\u5b9a\u4e49\u51fd\u6570\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def print_multiplication_table():<\/p>\n<p>    # \u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868<\/p>\n<p>    for i in range(1, 10):<\/p>\n<p>        for j in range(1, i + 1):<\/p>\n<p>            print(f&quot;{j}*{i}={i*j:2}&quot;, end=&quot; &quot;)<\/p>\n<p>        print()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u51fd\u6570\u5c01\u88c5\u540e\u7684\u4ee3\u7801\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u51fd\u6570\u6765\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8c03\u7528\u51fd\u6570\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868<\/p>\n<p>print_multiplication_table()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u51fd\u6570\u5c01\u88c5\uff0c\u53ef\u4ee5\u5c06\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868\u7684\u903b\u8f91\u72ec\u7acb\u51fa\u6765\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\u548c\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6269\u5c55\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u8f93\u51fa\u57fa\u7840\u7684\u4e58\u6cd5\u53e3\u8bc0\u8868\u5916\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u884c\u4e00\u4e9b\u6269\u5c55\u5e94\u7528\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8f93\u51fa\u66f4\u5927\u8303\u56f4\u7684\u4e58\u6cd5\u53e3\u8bc0\u8868\uff0c\u6216\u8005\u4ee5\u4e0d\u540c\u7684\u683c\u5f0f\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h4>\u8f93\u51fa\u66f4\u5927\u8303\u56f4\u7684\u4e58\u6cd5\u53e3\u8bc0\u8868<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u5faa\u73af\u7684\u8303\u56f4\u6765\u8f93\u51fa\u66f4\u5927\u8303\u56f4\u7684\u4e58\u6cd5\u53e3\u8bc0\u8868\u3002\u4f8b\u5982\uff0c\u8f93\u51fa\u4ece1\u523012\u7684\u4e58\u6cd5\u53e3\u8bc0\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def print_extended_multiplication_table():<\/p>\n<p>    # \u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868<\/p>\n<p>    for i in range(1, 13):<\/p>\n<p>        for j in range(1, i + 1):<\/p>\n<p>            print(f&quot;{j}*{i}={i*j:2}&quot;, end=&quot; &quot;)<\/p>\n<p>        print()<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u8f93\u51fa\u66f4\u5927\u8303\u56f4\u7684\u4e58\u6cd5\u53e3\u8bc0\u8868<\/strong><\/h2>\n<p>print_extended_multiplication_table()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4ee5\u4e0d\u540c\u683c\u5f0f\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u683c\u5f0f\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868\uff0c\u4f8b\u5982\u4ee5\u7ad6\u5411\u6392\u5217\u7684\u683c\u5f0f\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def print_vertical_multiplication_table():<\/p>\n<p>    # \u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868<\/p>\n<p>    for i in range(1, 10):<\/p>\n<p>        for j in range(1, 10):<\/p>\n<p>            print(f&quot;{i}*{j}={i*j:2}&quot;, end=&quot; &quot;)<\/p>\n<p>        print()<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u8f93\u51fa\u7ad6\u5411\u6392\u5217\u7684\u4e58\u6cd5\u53e3\u8bc0\u8868<\/strong><\/h2>\n<p>print_vertical_multiplication_table()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3\u4e0e\u63d0\u5347<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u5d4c\u5957\u5faa\u73af\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u548c\u51fd\u6570\u5c01\u88c5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0Python\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868\u7684\u529f\u80fd\u3002\u5d4c\u5957\u5faa\u73af\u7528\u4e8e\u904d\u5386\u884c\u548c\u5217\uff0c\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7528\u4e8e\u5bf9\u9f50\u8f93\u51fa\uff0c\u51fd\u6570\u5c01\u88c5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u3002\u901a\u8fc7\u6269\u5c55\u5e94\u7528\uff0c\u53ef\u4ee5\u8f93\u51fa\u66f4\u5927\u8303\u56f4\u7684\u4e58\u6cd5\u53e3\u8bc0\u8868\u6216\u4ee5\u4e0d\u540c\u683c\u5f0f\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u8fdb\u4e00\u6b65\u63d0\u5347\u4ee3\u7801\u7684\u8d28\u91cf\u548c\u529f\u80fd\uff0c\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u65b9\u9762\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u4f18\u5316\u4ee3\u7801\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u5e38\u89c1\u7684\u903b\u8f91\u63d0\u53d6\u6210\u72ec\u7acb\u7684\u51fd\u6570\uff0c\u4ece\u800c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u4e8e\u7ef4\u62a4\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u8f93\u51fa\u5355\u884c\u4e58\u6cd5\u8fd0\u7b97\u7684\u903b\u8f91\u63d0\u53d6\u6210\u4e00\u4e2a\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def print_multiplication_line(i):<\/p>\n<p>    for j in range(1, i + 1):<\/p>\n<p>        print(f&quot;{j}*{i}={i*j:2}&quot;, end=&quot; &quot;)<\/p>\n<p>    print()<\/p>\n<p>def print_multiplication_table():<\/p>\n<p>    for i in range(1, 10):<\/p>\n<p>        print_multiplication_line(i)<\/p>\n<p>print_multiplication_table()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u589e\u52a0\u7528\u6237\u4ea4\u4e92<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u589e\u52a0\u7528\u6237\u4ea4\u4e92\u529f\u80fd\uff0c\u8ba9\u7528\u6237\u8f93\u5165\u9700\u8981\u8f93\u51fa\u7684\u4e58\u6cd5\u53e3\u8bc0\u8868\u7684\u8303\u56f4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def print_custom_multiplication_table(n):<\/p>\n<p>    for i in range(1, n + 1):<\/p>\n<p>        for j in range(1, i + 1):<\/p>\n<p>            print(f&quot;{j}*{i}={i*j:2}&quot;, end=&quot; &quot;)<\/p>\n<p>        print()<\/p>\n<h2><strong>\u83b7\u53d6\u7528\u6237\u8f93\u5165<\/strong><\/h2>\n<p>n = int(input(&quot;\u8bf7\u8f93\u5165\u4e58\u6cd5\u53e3\u8bc0\u8868\u7684\u8303\u56f4: &quot;))<\/p>\n<p>print_custom_multiplication_table(n)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u9519\u8bef\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u589e\u52a0\u9519\u8bef\u5904\u7406\uff0c\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u8303\u56f4\u662f\u6709\u6548\u7684\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def print_custom_multiplication_table(n):<\/p>\n<p>    if n &lt; 1:<\/p>\n<p>        print(&quot;\u8303\u56f4\u5fc5\u987b\u5927\u4e8e0&quot;)<\/p>\n<p>        return<\/p>\n<p>    for i in range(1, n + 1):<\/p>\n<p>        for j in range(1, i + 1):<\/p>\n<p>            print(f&quot;{j}*{i}={i*j:2}&quot;, end=&quot; &quot;)<\/p>\n<p>        print()<\/p>\n<h2><strong>\u83b7\u53d6\u7528\u6237\u8f93\u5165<\/strong><\/h2>\n<p>try:<\/p>\n<p>    n = int(input(&quot;\u8bf7\u8f93\u5165\u4e58\u6cd5\u53e3\u8bc0\u8868\u7684\u8303\u56f4: &quot;))<\/p>\n<p>    print_custom_multiplication_table(n)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6709\u6548\u7684\u6574\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u6539\u8fdb\uff0c\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u5065\u58ee\u3001\u7075\u6d3b\u548c\u6613\u4e8e\u7ef4\u62a4\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u8ba9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u5b9e\u73b0Python\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u751f\u6210\u4e58\u6cd5\u53e3\u8bc0\u8868\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u751f\u6210\u4e58\u6cd5\u53e3\u8bc0\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u7684for\u5faa\u73af\u6765\u5b9e\u73b0\u3002\u5916\u5c42\u5faa\u73af\u63a7\u5236\u884c\u6570\uff0c\u5185\u5c42\u5faa\u73af\u63a7\u5236\u5217\u6570\u3002\u4ee3\u7801\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">for i in range(1, 10):\n    for j in range(1, i + 1):\n        print(f&quot;{j} x {i} = {i * j}&quot;, end=&#39;\\t&#39;)\n    print()\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u5c06\u8f93\u51fa1\u52309\u7684\u4e58\u6cd5\u53e3\u8bc0\u8868\u3002<\/p>\n<p><strong>\u6709\u4ec0\u4e48\u65b9\u6cd5\u53ef\u4ee5\u81ea\u5b9a\u4e49\u4e58\u6cd5\u53e3\u8bc0\u8868\u7684\u8303\u56f4\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u4ee3\u7801\u4e2d\u7684<code>range()<\/code>\u51fd\u6570\u53c2\u6570\u6765\u8c03\u6574\u4e58\u6cd5\u53e3\u8bc0\u8868\u7684\u8303\u56f4\u3002\u4f8b\u5982\uff0c\u82e5\u5e0c\u671b\u751f\u62101\u523012\u7684\u4e58\u6cd5\u53e3\u8bc0\u8868\uff0c\u53ef\u4ee5\u5c06\u4ee3\u7801\u4fee\u6539\u4e3a\uff1a  <\/p>\n<pre><code class=\"language-python\">for i in range(1, 13):\n    for j in range(1, i + 1):\n        print(f&quot;{j} x {i} = {i * j}&quot;, end=&#39;\\t&#39;)\n    print()\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u80fd\u751f\u62101\u523012\u7684\u4e58\u6cd5\u53e3\u8bc0\u8868\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u4e58\u6cd5\u53e3\u8bc0\u8868\u4fdd\u5b58\u4e3a\u6587\u672c\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u5c06\u4e58\u6cd5\u53e3\u8bc0\u8868\u4fdd\u5b58\u4e3a\u6587\u672c\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;multiplication_table.txt&#39;, &#39;w&#39;) as f:\n    for i in range(1, 10):\n        for j in range(1, i + 1):\n            f.write(f&quot;{j} x {i} = {i * j}\\t&quot;)\n        f.write(&quot;\\n&quot;)\n<\/code><\/pre>\n<p>\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\u540e\uff0c\u5c06\u751f\u6210\u4e00\u4e2a\u540d\u4e3a<code>multiplication_table.txt<\/code>\u7684\u6587\u4ef6\uff0c\u91cc\u9762\u5305\u542b\u4e86\u4e58\u6cd5\u53e3\u8bc0\u8868\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8f93\u51fa\u4e58\u6cd5\u53e3\u8bc0\u8868\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u5faa\u73af\u6765\u5b9e\u73b0\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5bf9\u9f50\u8f93\u51fa\u3001\u4f7f\u7528\u51fd\u6570\u5c01\u88c5\u4ee5\u589e\u5f3a\u4ee3\u7801\u590d\u7528\u6027\u3002\u63a5\u4e0b [&hellip;]","protected":false},"author":3,"featured_media":1182471,"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\/1182464"}],"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=1182464"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182464\/revisions"}],"predecessor-version":[{"id":1182474,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182464\/revisions\/1182474"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1182471"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1182464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1182464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1182464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}