{"id":204030,"date":"2025-05-29T09:29:56","date_gmt":"2025-05-29T01:29:56","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204030\/"},"modified":"2025-05-29T09:29:56","modified_gmt":"2025-05-29T01:29:56","slug":"%e6%88%91%e5%a6%82%e4%bd%95%e6%9e%84%e5%bb%ba%e6%88%91%e7%9a%84%e7%ac%ac%e4%b8%80%e4%b8%aa-python-pet-%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f%ef%bc%88%e4%bb%a5%e5%8f%8a%e6%88%91%e5%ad%a6%e5%88%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204030\/","title":{"rendered":"\u6211\u5982\u4f55\u6784\u5efa\u6211\u7684\u7b2c\u4e00\u4e2a Python PET \u5e94\u7528\u7a0b\u5e8f\uff08\u4ee5\u53ca\u6211\u5b66\u5230\u7684\u4e1c\u897f\uff09"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u6211\u5982\u4f55\u6784\u5efa\u6211\u7684\u7b2c\u4e00\u4e2a Python PET \u5e94\u7528\u7a0b\u5e8f\uff08\u4ee5\u53ca\u6211\u5b66\u5230\u7684\u4e1c\u897f\uff09<\/h1>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u6211\u5982\u4f55\u6784\u5efa\u6211\u7684\u7b2c\u4e00\u4e2a Python PET \u5e94\u7528\u7a0b\u5e8f\uff08\u4ee5\u53ca\u6211\u5b66\u5230\u7684\u4e1c\u897f\uff09\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241022\/17295750496717388952069.jpg\" class=\"aligncenter\"><\/p>\n<p>\u60a8\u597d\uff0cdev \u793e\u533a\uff01\u6211\u662f andre\uff0c\u4e00\u4f4d\u70ed\u8877\u4e8e\u6df1\u5165 python \u4e16\u754c\u7684\u521d\u5b66\u8005\u7a0b\u5e8f\u5458\u3002\u5728\u4e0e\u52a8\u529b\u6597\u4e89\u4e86\u51e0\u5e74\u4e4b\u540e\uff0c\u6211\u51b3\u5b9a\u5c06\u6ce8\u610f\u529b\u8f6c\u79fb\u5230\u6784\u5efa\u771f\u6b63\u7684\u9879\u76ee\u4e0a\u3002\u4eca\u5929\uff0c\u6211\u60f3\u5206\u4eab\u5173\u4e8e\u521b\u5efa\u6211\u7684\u7b2c\u4e00\u4e2a python \u9879\u76ee\u7684\u6545\u4e8b\uff1a\u4e2a\u4eba\u652f\u51fa\u8ddf\u8e2a\u5668 (pet) \u5e94\u7528\u7a0b\u5e8f\u3002 \uff08\u4ee3\u7801\u5728\u6700\u540e\uff09<\/p>\n<p>\u4e2a\u4eba\u5f00\u652f\u8ddf\u8e2a\u5668\u662f\u4e00\u6b3e\u547d\u4ee4\u884c\u5e94\u7528\u7a0b\u5e8f\uff0c\u65e8\u5728\u5e2e\u52a9\u7528\u6237\u8bb0\u5f55\u65e5\u5e38\u5f00\u652f\u3001\u5bf9\u5176\u8fdb\u884c\u5206\u7c7b\u5e76\u6df1\u5165\u4e86\u89e3\u4ed6\u4eec\u7684\u6d88\u8d39\u4e60\u60ef\u3002\u6211\u7684\u76ee\u6807\u662f\u521b\u5efa\u4e00\u79cd\u5de5\u5177\uff0c\u4f7f\u7528\u6237\u80fd\u591f\u63a7\u5236\u81ea\u5df1\u7684\u8d22\u52a1\u3002 \uff08\u8fd8\u6709\u6211\u7684\uff01\u554a\u54c8\uff09<\/p>\n<p>\u6211\u9762\u4e34\u7684\u6700\u91cd\u5927\u6311\u6218\u4e4b\u4e00\u662f\u5f04\u6e05\u695a\u5982\u4f55\u6709\u6548\u5730\u5b58\u50a8\u8d39\u7528\u6570\u636e\u3002\u6211\u6700\u521d\u5728 python \u4e2d\u5904\u7406\u6587\u4ef6\u65f6\u9047\u5230\u4e86\u56f0\u96be\uff0c\u4f46\u7ecf\u8fc7\u4e00\u756a\u575a\u6301\uff0c\u6211\u7ec8\u4e8e\u5b9e\u73b0\u4e86\u4e00\u4e2a\u53ef\u884c\u7684\u89e3\u51b3\u65b9\u6848\uff01<\/p>\n<p>\u901a\u8fc7\u8fd9\u4e2a\u9879\u76ee\uff0c\u6211\u4e86\u89e3\u4e86\u7528\u6237\u8f93\u5165\u9a8c\u8bc1\u548c\u786e\u4fdd\u6570\u636e\u8bb0\u5f55\u4e00\u81f4\u7684\u91cd\u8981\u6027\u3002\u6211\u8fd8\u83b7\u5f97\u4e86\u4f7f\u7528 python \u7ba1\u7406\u6587\u4ef6\u4ee5\u5b58\u50a8\u548c\u68c0\u7d22\u8d39\u7528\u8bb0\u5f55\u7684\u5b9d\u8d35\u7ecf\u9a8c\u3002<\/p>\n<p>\u5c55\u671b\u672a\u6765\uff0c\u6211\u8ba1\u5212\u96c6\u6210\u6570\u636e\u53ef\u89c6\u5316\u529f\u80fd\uff0c\u5e2e\u52a9\u7528\u6237\u76f4\u89c2\u5730\u770b\u5230\u4ed6\u4eec\u7684\u6d88\u8d39\u6a21\u5f0f\u3002\u53e6\u5916\uff0c\u6211\u5f88\u9ad8\u5174\u80fd\u591f\u5b9e\u73b0\u4e00\u4e2a\u9884\u7b97\u5de5\u5177\uff0c\u5141\u8bb8\u7528\u6237\u6309\u7c7b\u522b\u8bbe\u7f6e\u652f\u51fa\u9650\u989d\u3002<\/p>\n<p>\u5b8c\u6210\u4e2a\u4eba\u652f\u51fa\u8ddf\u8e2a\u5668\u662f\u4e00\u6b21\u975e\u5e38\u6709\u76ca\u7684\u7ecf\u5386\uff0c\u589e\u5f3a\u4e86\u6211\u4f5c\u4e3a\u5f00\u53d1\u4eba\u5458\u7684\u4fe1\u5fc3\u3002\u6211\u6e34\u671b\u7ee7\u7eed\u6211\u7684\u540e\u7aef\u5f00\u53d1\u548c devops \u5b66\u4e60\u4e4b\u65c5\uff0c\u66f4\u591a\u9879\u76ee\u5373\u5c06\u5230\u6765\uff01<\/p>\n<p>\u6211\u5f88\u60f3\u542c\u542c\u60a8\u7684\u53cd\u9988\uff01\u5982\u679c\u60a8\u6784\u5efa\u4e86\u7c7b\u4f3c\u7684\u4e1c\u897f\u6216\u6709\u589e\u5f3a\u8d39\u7528\u8ddf\u8e2a\u5668\u7684\u6280\u5de7\uff0c\u8bf7\u5206\u4eab\u60a8\u7684\u89c1\u89e3\uff01<\/p>\n<p>`<\/p>\n<p>def pet():<br \/> print(\u201c\u6b22\u8fce\u6765\u5230 pet\uff01\u201d)<br \/> print(\u201c\u60a8\u7684\u4e2a\u4eba\u5f00\u652f\u8ddf\u8e2a\u5668\uff0c\u5e2e\u52a9\u60a8\u8ddf\u8e2a\u60a8\u7684\u5f00\u652f\u3002\u201d)<br \/> print(&#8220;\u8d39\u7528\u7c7b\u522b\uff1a&#8221;)<br \/> print(&#8220;[1] \u98df\u54c1\u548c\u6742\u8d27&#8221;)<br \/> print(&#8220;[2] \u4ea4\u901a\uff08\u71c3\u6cb9\u3001\u516c\u5171\u4ea4\u901a\u7b49&#8230;\uff09&#8221;)<br \/> print(&#8220;[3] \u516c\u7528\u4e8b\u4e1a\uff08\u7535\u529b\u3001\u6c34\u3001\u4e92\u8054\u7f51\u7b49&#8230;\uff09&#8221;)<br \/> print(&#8220;[4] \u5a31\u4e50\u4f11\u95f2&#8221;)<br \/> print(&#8220;[5] \u533b\u7597\u4fdd\u5065\u548c\u533b\u7597\u8d39\u7528&#8221;)<br \/> print(&#8220;[6] \u79df\u91d1\u548c\u62b5\u62bc\u8d37\u6b3e&#8221;)<br \/> print(&#8220;[7] \u6742\u9879\uff08\u4efb\u4f55\u672a\u5206\u7c7b\u7684\u8d39\u7528\uff09&#8221;)<\/p>\n<pre>categories = [\n    \"Food &amp; Groceries\",\n    \"Transportation (Fuel, Public Transportation, etc...)\",\n    \"Utilities (Electricity, Water, Internet, etc...)\",\n    \"Entertainment &amp; Leisure\",\n    \"Healthcare &amp; Medical Expenses\",\n    \"Rent &amp; Mortgage\",\n    \"Miscellaneous (for any uncategorized expenses)\"\n]\n\nfood = []\ntransportation = []\nutilities = []\nentertainment = []\nhealthcare = []\nrent = []\nmiscs = []\n\nwhile True:\n    while True:\n        try:\n            choice = int(input(\"Select category: \"))\n            if 1 &lt;= choice &lt;= 7:\n                break\n            else:\n                raise ValueError\n        except ValueError:\n            return \"Choose a valid category!\"\n\n    while True:\n            try:\n                amount = float(input(\"Amount: \"))\n                break\n            except ValueError:\n                return \"Invalid number! Enter the amount of the expense.\"\n\n    if choice == 1:\n        food.append(amount)\n        print(f\"${amount} added to {categories[0]}\")\n    elif choice == 2:\n        transportation.append(amount)\n        print(f\"${amount} added to {categories[1]}\")\n    elif choice == 3:\n        utilities.append(amount)\n        print(f\"${amount} added to {categories[2]}\")\n    elif choice == 4:\n        entertainment.append(amount)\n        print(f\"${amount} added to {categories[3]}\")\n    elif choice == 5:\n        healthcare.append(amount)\n        print(f\"${amount} added to {categories[4]}\")\n    elif choice == 6:\n        rent.append(amount)\n        print(f\"${amount} added to {categories[5]}\")\n    elif choice == 7:\n        miscs.append(amount)\n        print(f\"${amount} added to {categories[6]}\")\n\n    option = input(\"Do you want to add another expense? (Y\/N)\").lower()\n\n    if option != 'y':\n        break\n    else: \n        continue\n\nfood_total = sum(food)\ntransportation_total = sum(transportation)\nutilities_total = sum(utilities)\nentertainment_total = sum(entertainment)\nhealthcare_total = sum(healthcare)\nrent_total = sum(rent)\nmiscs_total = sum(miscs)\n\nprint(\"Options:\")\nprint(\"[1] View total spent\")\nprint(\"[2] View total per category\")\n\nwhile True:\n    try:\n        show_expenses = int(input(\"Choose an option: \"))\n        if 1 &lt;= show_expenses &lt;= 2:\n            break\n        else:\n            raise ValueError\n    except ValueError:\n        return \"Invalid! Insert a valid option.\"\n\nif show_expenses == 1:\n    total_expenses = food_total + transportation_total + utilities_total + entertainment_total + healthcare_total + rent_total + miscs_total\n    print(f\"Your total expenses: ${total_expenses}\")\nelif show_expenses == 2:\n    print(f\"{categories[0]} total is: ${food_total}\")\n    print(f\"{categories[1]} total is: ${transportation_total}\")\n    print(f\"{categories[2]} total is: ${utilities_total}\")\n    print(f\"{categories[3]} total is: ${entertainment_total}\")\n    print(f\"{categories[4]} total is: ${healthcare_total}\")\n    print(f\"{categories[5]} total is: ${rent_total}\")\n    print(f\"{categories[6]} total is: ${miscs_total}\")\n<\/pre>\n<p>\u5ba0\u7269()<\/p>\n<p>`<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u6211\u5982\u4f55\u6784\u5efa\u6211\u7684\u7b2c\u4e00\u4e2a Python PET \u5e94\u7528\u7a0b\u5e8f\uff08\u4ee5\u53ca\u6211\u5b66\u5230\u7684\u4e1c\u897f\uff09\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u5982\u4f55\u6784\u5efa\u6211\u7684\u7b2c\u4e00\u4e2a Pytho&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204030","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204030","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204030"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204030\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204030"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204030"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204030"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}