{"id":1077529,"date":"2025-01-08T12:03:08","date_gmt":"2025-01-08T04:03:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1077529.html"},"modified":"2025-01-08T12:03:12","modified_gmt":"2025-01-08T04:03:12","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e9%81%8d%e5%8e%86%e4%b8%80%e4%b8%aa%e6%95%b4%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1077529.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u904d\u5386\u4e00\u4e2a\u6574\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181541\/d9a24582-844f-44e3-a6fd-cefaf56d7116.webp\" alt=\"python\u4e2d\u5982\u4f55\u904d\u5386\u4e00\u4e2a\u6574\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u904d\u5386\u4e00\u4e2a\u6574\u6570\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u5c06\u6574\u6570\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\u540e\u8fdb\u884c\u904d\u5386\u3001\u5229\u7528\u6570\u5b66\u8fd0\u7b97\u8fdb\u884c\u904d\u5386\u3001\u5c06\u6574\u6570\u8f6c\u5316\u4e3a\u5217\u8868\u540e\u8fdb\u884c\u904d\u5386\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a<strong>\u5c06\u6574\u6570\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\u540e\u8fdb\u884c\u904d\u5386<\/strong>\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u53ea\u9700\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u9010\u4e2a\u5b57\u7b26\u8fdb\u884c\u5904\u7406\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u83b7\u53d6\u6bcf\u4e00\u4f4d\u6570\u5b57\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u5c06\u6574\u6570\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\u540e\u904d\u5386<\/strong><\/h2>\n<p><h2>1\u3001\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u9010\u4e2a\u5b57\u7b26\u904d\u5386<\/h2>\n<\/p>\n<p><p>\u5c06\u6574\u6570\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u9010\u4e2a\u5b57\u7b26\u8fdb\u884c\u904d\u5386\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u5408\u5904\u7406\u5927\u591a\u6570\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 12345<\/p>\n<p>for digit in str(number):<\/p>\n<p>    print(digit)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>str(number)<\/code>\u5c06\u6574\u6570<code>number<\/code>\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u6bcf\u4e00\u4e2a\u5b57\u7b26\u4ee3\u8868\u6574\u6570\u4e2d\u7684\u4e00\u4e2a\u6570\u5b57\u3002<code>for digit in str(number)<\/code>\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u5e76\u5728\u5faa\u73af\u4e2d\u8fdb\u884c\u76f8\u5e94\u5904\u7406\uff0c\u6bd4\u5982\u6253\u5370\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u5904\u7406\u6bcf\u4e00\u4e2a\u5b57\u7b26<\/h2>\n<\/p>\n<p><p>\u5728\u904d\u5386\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u5bf9\u6bcf\u4e00\u4e2a\u5b57\u7b26\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u6bd4\u5982\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a\u6574\u6570\u3001\u7d2f\u52a0\u6c42\u548c\u3001\u5224\u65ad\u5947\u5076\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 12345<\/p>\n<p>total = 0<\/p>\n<p>for digit in str(number):<\/p>\n<p>    total += int(digit)<\/p>\n<p>print(&quot;Sum of digits:&quot;, total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5c06\u6bcf\u4e00\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3a\u6574\u6570\u540e\u8fdb\u884c\u7d2f\u52a0\uff0c\u6700\u7ec8\u8f93\u51fa\u6574\u6570\u5404\u4f4d\u6570\u5b57\u7684\u548c\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u5229\u7528\u6570\u5b66\u8fd0\u7b97\u904d\u5386\u6574\u6570<\/strong><\/h2>\n<p><h2>1\u3001\u9010\u4f4d\u63d0\u53d6\u6570\u5b57<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u6570\u5b66\u8fd0\u7b97\u53ef\u4ee5\u9010\u4f4d\u63d0\u53d6\u6574\u6570\u4e2d\u7684\u6bcf\u4e00\u4e2a\u6570\u5b57\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u53d6\u6a21\u8fd0\u7b97\u548c\u6574\u9664\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 12345<\/p>\n<p>while number &gt; 0:<\/p>\n<p>    digit = number % 10<\/p>\n<p>    print(digit)<\/p>\n<p>    number = number \/\/ 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>number % 10<\/code>\u83b7\u53d6\u6574\u6570\u7684\u6700\u540e\u4e00\u4f4d\u6570\u5b57\uff0c<code>number \/\/ 10<\/code>\u5c06\u6574\u6570\u53bb\u6389\u6700\u540e\u4e00\u4f4d\u6570\u5b57\u3002\u901a\u8fc7\u5faa\u73af\u53ef\u4ee5\u9010\u4f4d\u63d0\u53d6\u6570\u5b57\u5e76\u8fdb\u884c\u5904\u7406\uff0c\u6bd4\u5982\u6253\u5370\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u5904\u7406\u6bcf\u4e00\u4e2a\u6570\u5b57<\/h2>\n<\/p>\n<p><p>\u5728\u63d0\u53d6\u6bcf\u4e00\u4e2a\u6570\u5b57\u540e\uff0c\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u5404\u79cd\u5904\u7406\uff0c\u6bd4\u5982\u7d2f\u52a0\u6c42\u548c\u3001\u5224\u65ad\u5947\u5076\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 12345<\/p>\n<p>total = 0<\/p>\n<p>while number &gt; 0:<\/p>\n<p>    digit = number % 10<\/p>\n<p>    total += digit<\/p>\n<p>    number = number \/\/ 10<\/p>\n<p>print(&quot;Sum of digits:&quot;, total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u9010\u4f4d\u63d0\u53d6\u6570\u5b57\u5e76\u7d2f\u52a0\uff0c\u6700\u7ec8\u8f93\u51fa\u6574\u6570\u5404\u4f4d\u6570\u5b57\u7684\u548c\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u5c06\u6574\u6570\u8f6c\u5316\u4e3a\u5217\u8868\u540e\u904d\u5386<\/strong><\/h2>\n<p><h2>1\u3001\u8f6c\u6362\u4e3a\u5217\u8868\u5e76\u904d\u5386<\/h2>\n<\/p>\n<p><p>\u5c06\u6574\u6570\u8f6c\u5316\u4e3a\u5217\u8868\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u8fdb\u884c\u904d\u5386\u3002\u9996\u5148\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u518d\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 12345<\/p>\n<p>digits = list(str(number))<\/p>\n<p>for digit in digits:<\/p>\n<p>    print(digit)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>str(number)<\/code>\u5c06\u6574\u6570<code>number<\/code>\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c<code>list(str(number))<\/code>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u6bcf\u4e00\u4e2a\u5b57\u7b26\u4ee3\u8868\u6574\u6570\u4e2d\u7684\u4e00\u4e2a\u6570\u5b57\u3002<code>for digit in digits<\/code>\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u5e76\u5728\u5faa\u73af\u4e2d\u8fdb\u884c\u76f8\u5e94\u5904\u7406\uff0c\u6bd4\u5982\u6253\u5370\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u5904\u7406\u6bcf\u4e00\u4e2a\u6570\u5b57<\/h2>\n<\/p>\n<p><p>\u5728\u904d\u5386\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u5bf9\u6bcf\u4e00\u4e2a\u6570\u5b57\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u6bd4\u5982\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a\u6574\u6570\u3001\u7d2f\u52a0\u6c42\u548c\u3001\u5224\u65ad\u5947\u5076\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 12345<\/p>\n<p>digits = list(str(number))<\/p>\n<p>total = 0<\/p>\n<p>for digit in digits:<\/p>\n<p>    total += int(digit)<\/p>\n<p>print(&quot;Sum of digits:&quot;, total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5c06\u6bcf\u4e00\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3a\u6574\u6570\u540e\u8fdb\u884c\u7d2f\u52a0\uff0c\u6700\u7ec8\u8f93\u51fa\u6574\u6570\u5404\u4f4d\u6570\u5b57\u7684\u548c\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u4f7f\u7528\u9012\u5f52\u904d\u5386\u6574\u6570<\/strong><\/h2>\n<p><h2>1\u3001\u5b9a\u4e49\u9012\u5f52\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u9012\u5f52\u51fd\u6570\u4e5f\u53ef\u4ee5\u904d\u5386\u6574\u6570\u4e2d\u7684\u6bcf\u4e00\u4e2a\u6570\u5b57\u3002\u9012\u5f52\u51fd\u6570\u7684\u5b9a\u4e49\u9700\u8981\u5305\u542b\u7ec8\u6b62\u6761\u4ef6\u548c\u9012\u5f52\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def traverse_number(number):<\/p>\n<p>    if number == 0:<\/p>\n<p>        return<\/p>\n<p>    digit = number % 10<\/p>\n<p>    print(digit)<\/p>\n<p>    traverse_number(number \/\/ 10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>traverse_number<\/code>\u51fd\u6570\u901a\u8fc7\u9012\u5f52\u8c03\u7528\u81ea\u8eab\u9010\u4f4d\u63d0\u53d6\u6574\u6570\u4e2d\u7684\u6bcf\u4e00\u4e2a\u6570\u5b57\uff0c\u5e76\u5728\u63d0\u53d6\u540e\u8fdb\u884c\u76f8\u5e94\u5904\u7406\uff0c\u6bd4\u5982\u6253\u5370\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u5904\u7406\u6bcf\u4e00\u4e2a\u6570\u5b57<\/h2>\n<\/p>\n<p><p>\u5728\u9012\u5f52\u51fd\u6570\u4e2d\uff0c\u53ef\u4ee5\u5bf9\u6bcf\u4e00\u4e2a\u6570\u5b57\u8fdb\u884c\u5404\u79cd\u5904\u7406\uff0c\u6bd4\u5982\u7d2f\u52a0\u6c42\u548c\u3001\u5224\u65ad\u5947\u5076\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def sum_digits(number):<\/p>\n<p>    if number == 0:<\/p>\n<p>        return 0<\/p>\n<p>    digit = number % 10<\/p>\n<p>    return digit + sum_digits(number \/\/ 10)<\/p>\n<p>number = 12345<\/p>\n<p>total = sum_digits(number)<\/p>\n<p>print(&quot;Sum of digits:&quot;, total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u9012\u5f52\u51fd\u6570\u9010\u4f4d\u63d0\u53d6\u6570\u5b57\u5e76\u7d2f\u52a0\uff0c\u6700\u7ec8\u8f93\u51fa\u6574\u6570\u5404\u4f4d\u6570\u5b57\u7684\u548c\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u4f7f\u7528\u751f\u6210\u5668\u904d\u5386\u6574\u6570<\/strong><\/h2>\n<p><h2>1\u3001\u5b9a\u4e49\u751f\u6210\u5668\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9a\u4e49\u751f\u6210\u5668\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u904d\u5386\u6574\u6570\u4e2d\u7684\u6bcf\u4e00\u4e2a\u6570\u5b57\u3002\u751f\u6210\u5668\u51fd\u6570\u4f7f\u7528<code>yield<\/code>\u5173\u952e\u5b57\u8fd4\u56de\u6bcf\u4e00\u4e2a\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def digit_generator(number):<\/p>\n<p>    while number &gt; 0:<\/p>\n<p>        yield number % 10<\/p>\n<p>        number = number \/\/ 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>digit_generator<\/code>\u51fd\u6570\u901a\u8fc7<code>yield<\/code>\u5173\u952e\u5b57\u9010\u4f4d\u8fd4\u56de\u6574\u6570\u4e2d\u7684\u6bcf\u4e00\u4e2a\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u4f7f\u7528\u751f\u6210\u5668\u904d\u5386\u6570\u5b57<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u751f\u6210\u5668\u51fd\u6570\u8fd4\u56de\u7684\u6bcf\u4e00\u4e2a\u6570\u5b57\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u5904\u7406\uff0c\u6bd4\u5982\u6253\u5370\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 12345<\/p>\n<p>for digit in digit_generator(number):<\/p>\n<p>    print(digit)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>for digit in digit_generator(number)<\/code>\u904d\u5386\u751f\u6210\u5668\u51fd\u6570\u8fd4\u56de\u7684\u6bcf\u4e00\u4e2a\u6570\u5b57\uff0c\u5e76\u5728\u5faa\u73af\u4e2d\u8fdb\u884c\u76f8\u5e94\u5904\u7406\uff0c\u6bd4\u5982\u6253\u5370\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h2>3\u3001\u5904\u7406\u6bcf\u4e00\u4e2a\u6570\u5b57<\/h2>\n<\/p>\n<p><p>\u5728\u904d\u5386\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u5bf9\u6bcf\u4e00\u4e2a\u6570\u5b57\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u6bd4\u5982\u7d2f\u52a0\u6c42\u548c\u3001\u5224\u65ad\u5947\u5076\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def digit_generator(number):<\/p>\n<p>    while number &gt; 0:<\/p>\n<p>        yield number % 10<\/p>\n<p>        number = number \/\/ 10<\/p>\n<p>number = 12345<\/p>\n<p>total = 0<\/p>\n<p>for digit in digit_generator(number):<\/p>\n<p>    total += digit<\/p>\n<p>print(&quot;Sum of digits:&quot;, total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u9010\u4f4d\u63d0\u53d6\u6570\u5b57\u5e76\u7d2f\u52a0\uff0c\u6700\u7ec8\u8f93\u51fa\u6574\u6570\u5404\u4f4d\u6570\u5b57\u7684\u548c\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u4f7f\u7528\u9ad8\u7ea7\u6570\u636e\u7ed3\u6784\u904d\u5386\u6574\u6570<\/strong><\/h2>\n<p><h2>1\u3001\u4f7f\u7528\u961f\u5217\u904d\u5386\u6574\u6570<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u961f\u5217\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u5b9e\u73b0\u6574\u6570\u7684\u9010\u4f4d\u904d\u5386\u3002\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u540e\uff0c\u5c06\u6bcf\u4e00\u4e2a\u5b57\u7b26\u5165\u961f\uff0c\u7136\u540e\u9010\u4e2a\u51fa\u961f\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import deque<\/p>\n<p>number = 12345<\/p>\n<p>queue = deque(str(number))<\/p>\n<p>while queue:<\/p>\n<p>    digit = queue.popleft()<\/p>\n<p>    print(digit)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>str(number)<\/code>\u5c06\u6574\u6570<code>number<\/code>\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c<code>deque(str(number))<\/code>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u961f\u5217\uff0c\u6bcf\u4e00\u4e2a\u5b57\u7b26\u4ee3\u8868\u6574\u6570\u4e2d\u7684\u4e00\u4e2a\u6570\u5b57\u3002<code>queue.popleft()<\/code>\u9010\u4e2a\u51fa\u961f\u5e76\u8fdb\u884c\u76f8\u5e94\u5904\u7406\uff0c\u6bd4\u5982\u6253\u5370\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u5904\u7406\u6bcf\u4e00\u4e2a\u6570\u5b57<\/h2>\n<\/p>\n<p><p>\u5728\u904d\u5386\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u5bf9\u6bcf\u4e00\u4e2a\u6570\u5b57\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u6bd4\u5982\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a\u6574\u6570\u3001\u7d2f\u52a0\u6c42\u548c\u3001\u5224\u65ad\u5947\u5076\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import deque<\/p>\n<p>number = 12345<\/p>\n<p>queue = deque(str(number))<\/p>\n<p>total = 0<\/p>\n<p>while queue:<\/p>\n<p>    digit = queue.popleft()<\/p>\n<p>    total += int(digit)<\/p>\n<p>print(&quot;Sum of digits:&quot;, total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5c06\u6bcf\u4e00\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3a\u6574\u6570\u540e\u8fdb\u884c\u7d2f\u52a0\uff0c\u6700\u7ec8\u8f93\u51fa\u6574\u6570\u5404\u4f4d\u6570\u5b57\u7684\u548c\u3002<\/p>\n<\/p>\n<p><h2>3\u3001\u4f7f\u7528\u6808\u904d\u5386\u6574\u6570<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u6808\u6570\u636e\u7ed3\u6784\uff0c\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u6574\u6570\u7684\u9010\u4f4d\u904d\u5386\u3002\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u540e\uff0c\u5c06\u6bcf\u4e00\u4e2a\u5b57\u7b26\u5165\u6808\uff0c\u7136\u540e\u9010\u4e2a\u51fa\u6808\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 12345<\/p>\n<p>stack = list(str(number))<\/p>\n<p>while stack:<\/p>\n<p>    digit = stack.pop()<\/p>\n<p>    print(digit)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>str(number)<\/code>\u5c06\u6574\u6570<code>number<\/code>\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c<code>list(str(number))<\/code>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6808\uff0c\u6bcf\u4e00\u4e2a\u5b57\u7b26\u4ee3\u8868\u6574\u6570\u4e2d\u7684\u4e00\u4e2a\u6570\u5b57\u3002<code>stack.pop()<\/code>\u9010\u4e2a\u51fa\u6808\u5e76\u8fdb\u884c\u76f8\u5e94\u5904\u7406\uff0c\u6bd4\u5982\u6253\u5370\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h2>4\u3001\u5904\u7406\u6bcf\u4e00\u4e2a\u6570\u5b57<\/h2>\n<\/p>\n<p><p>\u5728\u904d\u5386\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u5bf9\u6bcf\u4e00\u4e2a\u6570\u5b57\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u6bd4\u5982\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a\u6574\u6570\u3001\u7d2f\u52a0\u6c42\u548c\u3001\u5224\u65ad\u5947\u5076\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 12345<\/p>\n<p>stack = list(str(number))<\/p>\n<p>total = 0<\/p>\n<p>while stack:<\/p>\n<p>    digit = stack.pop()<\/p>\n<p>    total += int(digit)<\/p>\n<p>print(&quot;Sum of digits:&quot;, total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5c06\u6bcf\u4e00\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3a\u6574\u6570\u540e\u8fdb\u884c\u7d2f\u52a0\uff0c\u6700\u7ec8\u8f93\u51fa\u6574\u6570\u5404\u4f4d\u6570\u5b57\u7684\u548c\u3002<\/p>\n<\/p>\n<h2><strong>\u4e03\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0Python\u4e2d\u904d\u5386\u4e00\u4e2a\u6574\u6570\u3002<strong>\u5c06\u6574\u6570\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\u540e\u904d\u5386\u3001\u5229\u7528\u6570\u5b66\u8fd0\u7b97\u8fdb\u884c\u904d\u5386\u3001\u5c06\u6574\u6570\u8f6c\u5316\u4e3a\u5217\u8868\u540e\u904d\u5386\u3001\u4f7f\u7528\u9012\u5f52\u904d\u5386\u6574\u6570\u3001\u4f7f\u7528\u751f\u6210\u5668\u904d\u5386\u6574\u6570\u3001\u4f7f\u7528\u9ad8\u7ea7\u6570\u636e\u7ed3\u6784\u904d\u5386\u6574\u6570<\/strong>\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u4f1a\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u4e0d\u540c\u7684\u65b9\u6cd5\u8fdb\u884c\u904d\u5386\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u8f83\u5927\u6574\u6570\u65f6\uff0c\u5229\u7528\u6570\u5b66\u8fd0\u7b97\u8fdb\u884c\u904d\u5386\u53ef\u80fd\u66f4\u4e3a\u9ad8\u6548\uff1b\u5728\u9700\u8981\u9012\u5f52\u5904\u7406\u65f6\uff0c\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u904d\u5386\u6574\u6570\u53ef\u80fd\u66f4\u4e3a\u65b9\u4fbf\uff1b\u5728\u9700\u8981\u4fdd\u5b58\u4e2d\u95f4\u72b6\u6001\u65f6\uff0c\u4f7f\u7528\u751f\u6210\u5668\u6216\u9ad8\u7ea7\u6570\u636e\u7ed3\u6784\u904d\u5386\u6574\u6570\u53ef\u80fd\u66f4\u4e3a\u5408\u9002\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1Python\u4e2d\u904d\u5386\u6574\u6570\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u904d\u5386\u4e00\u4e2a\u6574\u6570\u7684\u6bcf\u4e00\u4f4d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u6765\u904d\u5386\u5176\u6bcf\u4e00\u4f4d\u3002\u4f7f\u7528<code>str()<\/code>\u51fd\u6570\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u540e\uff0c\u4fbf\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u9010\u4f4d\u8bbf\u95ee\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u6574\u657012345\uff0c\u53ef\u4ee5\u8fd9\u6837\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">num = 12345\nfor digit in str(num):\n    print(digit)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u8f93\u51fa\u5c06\u662f\u6bcf\u4e00\u4f4d\u6570\u5b57\uff1a1, 2, 3, 4, 5\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u6570\u5b66\u65b9\u6cd5\u904d\u5386\u6574\u6570\u7684\u6bcf\u4e00\u4f4d\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u5b66\u65b9\u6cd5\u6765\u904d\u5386\u6574\u6570\u3002\u901a\u8fc7\u4e0d\u65ad\u53d6\u6a21\u548c\u6574\u9664\u7684\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u63d0\u53d6\u51fa\u6bcf\u4e00\u4f4d\u6570\u5b57\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">num = 12345\nwhile num &gt; 0:\n    digit = num % 10\n    print(digit)\n    num \/\/= 10\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u4ece\u4e2a\u4f4d\u5f00\u59cb\u8f93\u51fa\u6570\u5b57\uff0c\u8f93\u51fa\u987a\u5e8f\u4e3a5, 4, 3, 2, 1\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u8d1f\u6574\u6570\u6216\u96f6\u7684\u904d\u5386\uff1f<\/strong><br \/>\u5bf9\u4e8e\u8d1f\u6574\u6570\uff0c\u9996\u5148\u53ef\u4ee5\u53d6\u7edd\u5bf9\u503c\u8fdb\u884c\u904d\u5386\u3002\u5904\u7406\u96f6\u65f6\uff0c\u60a8\u53ea\u9700\u5355\u72ec\u5224\u65ad\u5e76\u8f93\u51fa0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5904\u7406\u8d1f\u6570\u548c\u96f6\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">num = -12345\nif num == 0:\n    print(0)\nelse:\n    for digit in str(abs(num)):\n        print(digit)\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5bf9\u4e8e\u8d1f\u6570\uff0c\u4f7f\u7528<code>abs()<\/code>\u51fd\u6570\u83b7\u53d6\u5176\u7edd\u5bf9\u503c\u518d\u8fdb\u884c\u904d\u5386\uff0c\u786e\u4fdd\u8f93\u51fa\u7684\u90fd\u662f\u6b63\u4f4d\u6570\u5b57\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u904d\u5386\u4e00\u4e2a\u6574\u6570\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u5c06\u6574\u6570\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\u540e\u8fdb\u884c\u904d\u5386\u3001\u5229\u7528\u6570\u5b66\u8fd0\u7b97\u8fdb\u884c\u904d\u5386\u3001\u5c06\u6574\u6570\u8f6c\u5316\u4e3a\u5217\u8868 [&hellip;]","protected":false},"author":3,"featured_media":1077544,"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\/1077529"}],"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=1077529"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1077529\/revisions"}],"predecessor-version":[{"id":1077545,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1077529\/revisions\/1077545"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1077544"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1077529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1077529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1077529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}