{"id":1132311,"date":"2025-01-08T20:55:58","date_gmt":"2025-01-08T12:55:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1132311.html"},"modified":"2025-01-08T20:56:00","modified_gmt":"2025-01-08T12:56:00","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%bc%96%e7%a8%8b%e4%b9%9d%e4%b9%9d%e4%b9%98%e6%b3%95%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1132311.html","title":{"rendered":"\u5982\u4f55\u7528python\u7f16\u7a0b\u4e5d\u4e5d\u4e58\u6cd5\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101903\/f980b72e-f2c5-4e66-a9fa-39687f194dfd.webp\" alt=\"\u5982\u4f55\u7528python\u7f16\u7a0b\u4e5d\u4e5d\u4e58\u6cd5\u8868\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u7528Python\u7f16\u7a0b\u4e5d\u4e5d\u4e58\u6cd5\u8868<\/strong><\/p>\n<\/p>\n<p><p>\u7528Python\u7f16\u7a0b\u4e5d\u4e5d\u4e58\u6cd5\u8868\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5173\u952e\u5728\u4e8e\u638c\u63e1<strong>for\u5faa\u73af\u3001\u5d4c\u5957\u5faa\u73af\u3001\u683c\u5f0f\u5316\u8f93\u51fa<\/strong>\u7b49\u57fa\u7840\u6982\u5ff5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u8bb2\u89e3\u5176\u4e2d\u4e00\u79cd\u5b9e\u73b0\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4e5d\u4e5d\u4e58\u6cd5\u8868\u7684\u57fa\u672c\u6982\u5ff5<\/h3>\n<\/p>\n<p><p>\u4e5d\u4e5d\u4e58\u6cd5\u8868\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u6570\u5b66\u5de5\u5177\uff0c\u7528\u4e8e\u5c55\u793a1\u52309\u4e4b\u95f4\u6240\u6709\u6570\u5b57\u7684\u4e58\u79ef\u3002\u5b83\u4e0d\u4ec5\u662f\u5c0f\u5b66\u6570\u5b66\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u4e5f\u662f\u7f16\u7a0b\u7ec3\u4e60\u7684\u7ecf\u5178\u9898\u76ee\u3002\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u7684for\u5faa\u73af\u6765\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u7528for\u5faa\u73af\u5b9e\u73b0\u4e5d\u4e5d\u4e58\u6cd5\u8868<\/h3>\n<\/p>\n<p><h4>1. \u57fa\u672c\u539f\u7406<\/h4>\n<\/p>\n<p><p>\u7528Python\u7f16\u7a0b\u4e5d\u4e5d\u4e58\u6cd5\u8868\u7684\u6838\u5fc3\u5728\u4e8e\u4f7f\u7528\u5d4c\u5957\u7684for\u5faa\u73af\u3002\u5916\u5c42\u5faa\u73af\u63a7\u5236\u884c\u6570\uff0c\u5185\u5c42\u5faa\u73af\u63a7\u5236\u5217\u6570\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u7b2ci\u884c\uff0c\u6211\u4eec\u9700\u8981\u4ece1\u4e58\u5230i\uff0c\u7ed3\u679c\u6309\u5e8f\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(1, 10):  # \u5916\u5c42\u5faa\u73af\u63a7\u5236\u884c\u6570<\/p>\n<p>    for j in range(1, i + 1):  # \u5185\u5c42\u5faa\u73af\u63a7\u5236\u5217\u6570<\/p>\n<p>        print(f&quot;{i}*{j}={i*j}&quot;, end=&quot;\\t&quot;)  # \u4f7f\u7528\u5236\u8868\u7b26\u8fdb\u884c\u683c\u5f0f\u5316\u8f93\u51fa<\/p>\n<p>    print()  # \u6bcf\u884c\u8f93\u51fa\u5b8c\u540e\u6362\u884c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8be6\u7ec6\u8bb2\u89e3<\/h4>\n<\/p>\n<ul>\n<li><strong>\u5916\u5c42\u5faa\u73af<\/strong>\uff1a<code>for i in range(1, 10)<\/code>\uff0c\u4ece1\u52309\uff0c\u51719\u884c\u3002<\/li>\n<li><strong>\u5185\u5c42\u5faa\u73af<\/strong>\uff1a<code>for j in range(1, i + 1)<\/code>\uff0c\u4ece1\u4e58\u5230i\uff0c\u7ed3\u679c\u6309\u5e8f\u8f93\u51fa\u3002<\/li>\n<li><strong>\u683c\u5f0f\u5316\u8f93\u51fa<\/strong>\uff1a<code>print(f&quot;{i}*{j}={i*j}&quot;, end=&quot;\\t&quot;)<\/code>\uff0c\u4f7f\u7528\u5236\u8868\u7b26<code>\\t<\/code>\u6765\u5bf9\u9f50\u8f93\u51fa\u3002<\/li>\n<li><strong>\u6362\u884c<\/strong>\uff1a\u6bcf\u884c\u8f93\u51fa\u5b8c\u540e\u8fdb\u884c\u6362\u884c\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f18\u5316\u8f93\u51fa\u683c\u5f0f<\/h3>\n<\/p>\n<p><h4>1. \u5bf9\u9f50\u8f93\u51fa<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u8ba9\u4e58\u6cd5\u8868\u770b\u8d77\u6765\u66f4\u7f8e\u89c2\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>ljust()<\/code>\u65b9\u6cd5\u8fdb\u884c\u5bf9\u9f50\u3002<code>ljust(width)<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5de6\u5bf9\u9f50\uff0c\u5e76\u4f7f\u7528\u7a7a\u683c\u586b\u5145\u81f3\u6307\u5b9a\u5bbd\u5ea6\u3002<\/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;{i}*{j}={i*j}&quot;.ljust(8), end=&quot;&quot;)  # \u6bcf\u4e2a\u8868\u8fbe\u5f0f\u53608\u4e2a\u5b57\u7b26\u5bbd\u5ea6<\/p>\n<p>    print()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u5217\u8868\u751f\u6210\u5f0f<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u4f20\u7edf\u7684for\u5faa\u73af\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5229\u7528\u5217\u8868\u751f\u6210\u5f0f\u6765\u751f\u6210\u4e5d\u4e5d\u4e58\u6cd5\u8868\u7684\u6bcf\u4e00\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(1, 10):<\/p>\n<p>    line = [f&quot;{i}*{j}={i*j}&quot;.ljust(8) for j in range(1, i + 1)]<\/p>\n<p>    print(&quot;&quot;.join(line))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5c06\u4e5d\u4e5d\u4e58\u6cd5\u8868\u4fdd\u5b58\u5230\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u751f\u6210\u7684\u4e5d\u4e5d\u4e58\u6cd5\u8868\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570<code>open()<\/code>\u3001<code>write()<\/code>\u7b49\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;multiplication_table.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    for i in range(1, 10):<\/p>\n<p>        line = [f&quot;{i}*{j}={i*j}&quot;.ljust(8) for j in range(1, i + 1)]<\/p>\n<p>        file.write(&quot;&quot;.join(line) + &quot;\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u7528\u51fd\u6570\u5c01\u88c5\u4e5d\u4e5d\u4e58\u6cd5\u8868<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u751f\u6210\u4e5d\u4e5d\u4e58\u6cd5\u8868\u7684\u4ee3\u7801\u5c01\u88c5\u6210\u4e00\u4e2a\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def print_multiplication_table():<\/p>\n<p>    for i in range(1, 10):<\/p>\n<p>        line = [f&quot;{i}*{j}={i*j}&quot;.ljust(8) for j in range(1, i + 1)]<\/p>\n<p>        print(&quot;&quot;.join(line))<\/p>\n<p>print_multiplication_table()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u518d\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def save_multiplication_table(filename):<\/p>\n<p>    with open(filename, &quot;w&quot;) as file:<\/p>\n<p>        for i in range(1, 10):<\/p>\n<p>            line = [f&quot;{i}*{j}={i*j}&quot;.ljust(8) for j in range(1, i + 1)]<\/p>\n<p>            file.write(&quot;&quot;.join(line) + &quot;\\n&quot;)<\/p>\n<p>save_multiplication_table(&quot;multiplication_table.txt&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5176\u4ed6\u5b9e\u73b0\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528while\u5faa\u73af<\/h4>\n<\/p>\n<p><p>\u9664\u4e86for\u5faa\u73af\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528while\u5faa\u73af\u6765\u5b9e\u73b0\u4e5d\u4e5d\u4e58\u6cd5\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">i = 1<\/p>\n<p>while i &lt; 10:<\/p>\n<p>    j = 1<\/p>\n<p>    while j &lt;= i:<\/p>\n<p>        print(f&quot;{i}*{j}={i*j}&quot;, end=&quot;\\t&quot;)<\/p>\n<p>        j += 1<\/p>\n<p>    print()<\/p>\n<p>    i += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u9012\u5f52<\/h4>\n<\/p>\n<p><p>\u867d\u7136\u9012\u5f52\u4e0d\u662f\u751f\u6210\u4e5d\u4e5d\u4e58\u6cd5\u8868\u7684\u5e38\u89c1\u65b9\u6cd5\uff0c\u4f46\u5b83\u4e5f\u662f\u4e00\u79cd\u53ef\u4ee5\u5b9e\u73b0\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def print_multiplication_table_recursive(i=1, j=1):<\/p>\n<p>    if i &gt; 9:<\/p>\n<p>        return<\/p>\n<p>    if j &lt;= i:<\/p>\n<p>        print(f&quot;{i}*{j}={i*j}&quot;, end=&quot;\\t&quot;)<\/p>\n<p>        print_multiplication_table_recursive(i, j + 1)<\/p>\n<p>    else:<\/p>\n<p>        print()<\/p>\n<p>        print_multiplication_table_recursive(i + 1, 1)<\/p>\n<p>print_multiplication_table_recursive()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u7528Python\u7f16\u7a0b\u4e5d\u4e5d\u4e58\u6cd5\u8868\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5173\u952e\u5728\u4e8e\u638c\u63e1<strong>for\u5faa\u73af\u3001\u5d4c\u5957\u5faa\u73af\u3001\u683c\u5f0f\u5316\u8f93\u51fa<\/strong>\u7b49\u57fa\u7840\u6982\u5ff5\u3002\u901a\u8fc7\u4e0a\u9762\u7684\u8bb2\u89e3\uff0c\u76f8\u4fe1\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u751f\u6210\u4e5d\u4e5d\u4e58\u6cd5\u8868\uff0c\u5e76\u4e14\u80fd\u591f\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8fdb\u884c\u4f18\u5316\u548c\u6269\u5c55\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u4f60\u7f16\u7a0b\u6109\u5feb\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u4e5d\u4e5d\u4e58\u6cd5\u8868\u7684\u6253\u5370\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b80\u5355\u7684\u5faa\u73af\u7ed3\u6784\u6765\u5b9e\u73b0\u4e5d\u4e5d\u4e58\u6cd5\u8868\u7684\u6253\u5370\u3002\u901a\u8fc7\u5d4c\u5957\u5faa\u73af\uff0c\u53ef\u4ee5\u9010\u884c\u8f93\u51fa\u6bcf\u4e00\u884c\u7684\u4e58\u6cd5\u7ed3\u679c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u4e24\u4e2afor\u5faa\u73af\u6765\u904d\u53861\u52309\u7684\u6570\u5b57\uff0c\u5e76\u8ba1\u7b97\u6bcf\u4e00\u5bf9\u6570\u5b57\u7684\u4e58\u79ef\uff0c\u6700\u540e\u5c06\u7ed3\u679c\u683c\u5f0f\u5316\u8f93\u51fa\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u7f16\u5199\u4e5d\u4e5d\u4e58\u6cd5\u8868\u65f6\uff0c\u662f\u5426\u6709\u63a8\u8350\u7684\u4ee3\u7801\u7ed3\u6784\uff1f<\/strong><br \/>\u63a8\u8350\u4f7f\u7528\u51fd\u6570\u6765\u5c01\u88c5\u4e5d\u4e5d\u4e58\u6cd5\u8868\u7684\u903b\u8f91\uff0c\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u4f8b\u5982<code>print_multiplication_table()<\/code>\uff0c\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u6765\u751f\u6210\u4e58\u6cd5\u8868\uff0c\u8c03\u7528\u8be5\u51fd\u6570\u5373\u53ef\u8f93\u51fa\u8868\u683c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5b9e\u73b0\u4e5d\u4e5d\u4e58\u6cd5\u8868\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u548c\u89e3\u51b3\u65b9\u6cd5\uff1f<\/strong><br \/>\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u5faa\u73af\u8303\u56f4\u8bbe\u7f6e\u4e0d\u5f53\u3001\u683c\u5f0f\u5316\u8f93\u51fa\u4e0d\u6b63\u786e\u7b49\u3002\u786e\u4fdd\u5faa\u73af\u7684\u8303\u56f4\u4e3a1\u52309\uff0c\u5e76\u5728\u6253\u5370\u65f6\u4f7f\u7528\u5408\u9002\u7684\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5982<code>print(f&quot;{i} x {j} = {i * j}&quot;, end=&quot;\\t&quot;)<\/code>\uff0c\u4ee5\u786e\u4fdd\u8f93\u51fa\u6574\u9f50\u3002\u5982\u679c\u51fa\u73b0\u4e86\u9519\u8bef\uff0c\u68c0\u67e5\u5faa\u73af\u6761\u4ef6\u548c\u8f93\u51fa\u8bed\u53e5\u7684\u8bed\u6cd5\u683c\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7528Python\u7f16\u7a0b\u4e5d\u4e5d\u4e58\u6cd5\u8868 \u7528Python\u7f16\u7a0b\u4e5d\u4e5d\u4e58\u6cd5\u8868\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5173\u952e\u5728\u4e8e\u638c\u63e1for\u5faa\u73af\u3001\u5d4c\u5957\u5faa\u73af [&hellip;]","protected":false},"author":3,"featured_media":1132318,"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\/1132311"}],"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=1132311"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132311\/revisions"}],"predecessor-version":[{"id":1132320,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132311\/revisions\/1132320"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1132318"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1132311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1132311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1132311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}