{"id":3544,"date":"2025-11-22T17:38:34","date_gmt":"2025-11-22T09:38:34","guid":{"rendered":"https:\/\/tobykskgd.life\/?p=3544"},"modified":"2025-11-22T17:40:32","modified_gmt":"2025-11-22T09:40:32","slug":"120","status":"publish","type":"post","link":"https:\/\/tobykskgd.life\/index.php\/120\/","title":{"rendered":"CS50P Problem Set 3"},"content":{"rendered":"\n<p>\u8bfe\u7a0b\uff1a<a href=\"https:\/\/cs50.harvard.edu\/python\/\">CS50\u2019s Introduction to Programming with Python<\/a><\/p>\n\n\n\n<p>CS50 Python 2025 \u8bfe\u7a0b\u7684\u95ee\u9898\u96c6\u4f5c\u4e1a\u63d0\u4ea4\u3002\u4e3b\u8981\u63d0\u4f9b\u4e00\u4e2a\u672c\u4eba\u7684\u89e3\u9898\u601d\u8def\uff0c\u4ec5\u4f9b\u53c2\u8003\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Fuel Gauge<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Problem<\/h4>\n\n\n\n<p>Fuel gauges indicate, often with fractions, just how much fuel is in a tank. For instance 1\/4 indicates that a tank is 25% full, 1\/2 indicates that a tank is 50% full, and 3\/4 indicates that a tank is 75% full.<br>\u71c3\u6cb9\u8868\u901a\u5e38\u4ee5\u5206\u6570\u8868\u793a\u6cb9\u7bb1\u5185\u7684\u71c3\u6cb9\u91cf\u3002\u4f8b\u5982\uff0c1\/4\u8868\u793a\u6cb9\u7bb1\u5df2\u6ee125%\uff0c1\/2\u8868\u793a\u6cb9\u7bb1\u5df2\u6ee150%\uff0c3\/4\u8868\u793a\u6cb9\u7bb1\u6cb9\u91cf\u4e3a75%\u3002<\/p>\n\n\n\n<p>In a file called&nbsp;<code>fuel.py<\/code>, implement a program that prompts the user for a fraction, formatted as&nbsp;<code>X\/Y<\/code>, wherein each of&nbsp;<code>X<\/code>&nbsp;and&nbsp;<code>Y<\/code>&nbsp;is a positive integer, and then outputs, as a percentage rounded to the nearest integer, how much fuel is in the tank. If, though, 1% or less remains, output&nbsp;<code>E<\/code>&nbsp;instead to indicate that the tank is essentially empty. And if 99% or more remains, output&nbsp;<code>F<\/code>&nbsp;instead to indicate that the tank is essentially full.<br>\u5728\u4e00\u4e2a\u540d\u4e3a&nbsp;<code>fuel.py<\/code>&nbsp;\u7684\u6587\u4ef6\u4e2d\uff0c\u5b9e\u73b0\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u5206\u6570\uff0c\u683c\u5f0f\u4e3a&nbsp;<code>X\/Y<\/code>\uff0c\u5176\u4e2d&nbsp;<code>X<\/code>&nbsp;\u548c&nbsp;<code>Y<\/code>&nbsp;\u5404\u4e3a\u6b63\u6574\u6570\uff0c\u7136\u540e\u8f93\u51fa\u6cb9\u7bb1\u5185\u71c3\u6599\u7684\u767e\u5206\u6bd4\uff0c\u56db\u820d\u4e94\u5165\u81f3\u6700\u63a5\u8fd1\u7684\u6574\u6570\u3002\u5982\u679c\u5269\u4f59 1%\u6216\u66f4\u5c11\uff0c\u5219\u8f93\u51fa&nbsp;<code>E<\/code>\uff0c\u8868\u793a\u6cb9\u7bb1\u57fa\u672c\u7a7a\u4e86\u3002\u5982\u679c\u5269\u4f59 99%\u6216\u66f4\u591a\uff0c\u5219\u8f93\u51fa&nbsp;<code>F<\/code>\uff0c\u8868\u793a\u6cb9\u7bb1\u57fa\u672c\u6ee1\u3002<\/p>\n\n\n\n<p>If, though,&nbsp;<code>X<\/code>&nbsp;or&nbsp;<code>Y<\/code>&nbsp;is not an integer,&nbsp;<code>X<\/code>&nbsp;is greater than&nbsp;<code>Y<\/code>, or&nbsp;<code>Y<\/code>&nbsp;is&nbsp;<code>0<\/code>, instead prompt the user again. (It is not necessary for&nbsp;<code>Y<\/code>&nbsp;to be&nbsp;<code>4<\/code>.) Be sure to catch any exceptions like&nbsp;<a href=\"https:\/\/docs.python.org\/3\/library\/exceptions.html#ValueError\"><code>ValueError<\/code><\/a>&nbsp;or&nbsp;<a href=\"https:\/\/docs.python.org\/3\/library\/exceptions.html#ZeroDivisionError\"><code>ZeroDivisionError<\/code><\/a>.<br>\u4f46\u5982\u679c&nbsp;<code>X<\/code>&nbsp;\u6216&nbsp;<code>Y<\/code>&nbsp;\u4e0d\u662f\u6574<code>\u6570\uff0cX<\/code>&nbsp;\u5927\u4e8e&nbsp;<code>Y<\/code>\uff0c\u6216&nbsp;<code>Y<\/code>&nbsp;\u662f&nbsp;<code>0<\/code>\uff0c\u5219\u518d\u6b21\u63d0\u793a\u7528\u6237\u3002\uff08<code>Y<\/code>&nbsp;\u4e0d\u5fc5\u662f&nbsp;<code>4<\/code>\u3002\uff09\u52a1\u5fc5\u6355\u6349\u5f02\u5e38\uff0c\u5982&nbsp;<a href=\"https:\/\/docs.python.org\/3\/library\/exceptions.html#ValueError\"><code>ValueError<\/code><\/a>&nbsp;\u6216&nbsp;<a href=\"https:\/\/docs.python.org\/3\/library\/exceptions.html#ZeroDivisionError\"><code>ZeroDivisionError<\/code><\/a>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Submit<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>def main():\n    while True:\n        try:\n            fraction = input(\"Fraction: \")\n            x_str, y_str = fraction.split(\"\/\")\n            x = int(x_str)\n            y = int(y_str)\n            if 0 &lt;= x &lt;= y:\n                fuel = round(x \/ y * 100)\n                if fuel &lt;= 1:\n                    print(\"E\")\n                elif fuel >= 99:\n                    print(\"F\")\n                else:\n                    print(f\"{fuel}%\")\n                break\n        except (ValueError, ZeroDivisionError):\n            pass\n\nmain()\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Tips<\/h4>\n\n\n\n<p><strong><code>int()<\/code> \u9047\u5230\u5e26\u5c0f\u6570\u70b9\u7684\u5b57\u7b26\u4e32\u4f1a\u629b\u51fa ValueError<\/strong><\/p>\n\n\n\n<p>\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int(\"1.5\")<\/code><\/pre>\n\n\n\n<p>\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ValueError: invalid literal for int() with base 10: '1.5'<\/code><\/pre>\n\n\n\n<p>\u539f\u56e0\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>int()<\/code> \u53ea\u80fd\u63a5\u53d7 <strong>\u7eaf\u6574\u6570\u683c\u5f0f\u7684\u5b57\u7b26\u4e32<\/strong>\uff08\u5982 <code>\"3\"<\/code>, <code>\"-5\"<\/code>\uff09<\/li>\n\n\n\n<li><code>\"1.5\"<\/code> <strong>\u4e0d\u662f\u6574\u6570\u683c\u5f0f<\/strong>\uff0c\u56e0\u6b64\u4f1a\u629b\u51fa ValueError<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Felipe\u2019s Taqueria<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Problem<\/h4>\n\n\n\n<p>One of the most popular places to eat in&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Harvard_Square\">Harvard Square<\/a>&nbsp;is&nbsp;<a href=\"https:\/\/www.felipesboston.com\/\">Felipe\u2019s Taqueria<\/a>, which offers a&nbsp;<a href=\"https:\/\/www.felipesboston.com\/menu\">menu<\/a>&nbsp;of entrees, per the&nbsp;<code>dict<\/code>&nbsp;below, wherein the value of each key is a price in dollars:<br><a href=\"https:\/\/en.wikipedia.org\/wiki\/Harvard_Square\">\u54c8\u4f5b\u5e7f\u573a<\/a>\u6700\u53d7\u6b22\u8fce\u7684\u9910\u5385\u4e4b\u4e00\u662f&nbsp;<a href=\"https:\/\/www.felipesboston.com\/\">Felipe&#8217;s Taqueria<\/a>\uff0c\u6839\u636e\u4e0b\u9762\u7684<code>\u89c4\u5b9a&nbsp;<\/code>\uff0c\u63d0\u4f9b\u4e00\u7cfb\u5217\u4e3b\u83dc\uff0c\u6bcf\u628a\u94a5\u5319\u7684<a href=\"https:\/\/www.felipesboston.com\/menu\"><\/a>\u4ef7\u503c\u4ee5\u7f8e\u5143\u8ba1\u4ef7\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n    \"Baja Taco\": 4.25,\n    \"Burrito\": 7.50,\n    \"Bowl\": 8.50,\n    \"Nachos\": 11.00,\n    \"Quesadilla\": 8.50,\n    \"Super Burrito\": 8.50,\n    \"Super Quesadilla\": 9.50,\n    \"Taco\": 3.00,\n    \"Tortilla Salad\": 8.00\n}\n<\/code><\/pre>\n\n\n\n<p>In a file called&nbsp;<code>taqueria.py<\/code>, implement a program that enables a user to place an order, prompting them for items, one per line, until the user inputs control-d (which is a common way of ending one\u2019s input to a program). After each inputted item, display the total cost of all items inputted thus far, prefixed with a dollar sign (<code>$<\/code>) and formatted to two decimal places. Treat the user\u2019s input case insensitively. Ignore any input that isn\u2019t an item. Assume that every item on the menu will be&nbsp;<a href=\"https:\/\/docs.python.org\/3\/library\/stdtypes.html#str.title\">titlecased<\/a>.<br>\u5728\u4e00\u4e2a\u540d\u4e3a&nbsp;<code>taqueria.py<\/code>&nbsp;\u7684\u6587\u4ef6\u4e2d\uff0c\u5b9e\u73b0\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u5141\u8bb8\u7528\u6237\u4e0b\u5355\uff0c\u63d0\u793a\u4ed6\u4eec\u6bcf\u884c\u4e00\u4e2a\u7269\u54c1\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165 control-d\uff08\u8fd9\u662f\u7ed3\u675f\u7a0b\u5e8f\u8f93\u5165\u7684\u5e38\u89c1\u65b9\u5f0f\uff09\u3002\u6bcf\u8f93\u5165\u4e00\u9879\u540e\uff0c\u663e\u793a\u8fc4\u4eca\u4e3a\u6b62\u6240\u6709\u8f93\u5165\u7269\u54c1\u7684\u603b\u8d39\u7528\uff0c\u524d\u7f6e\u4e00\u4e2a\u7f8e\u5143\u7b26\u53f7\uff08<code>$<\/code>\uff09\uff0c\u683c\u5f0f\u4e3a\u5c0f\u6570\u70b9\u540e\u4e24\u4f4d\u3002\u5bf9\u7528\u6237\u8f93\u5165\u5927\u5c0f\u5199\u4e0d\u654f\u611f\u3002\u5ffd\u7565\u4efb\u4f55\u975e\u7269\u54c1\u7684\u8f93\u5165\u3002\u5047\u8bbe\u83dc\u5355\u4e0a\u7684\u6bcf\u4e00\u9879\u90fd\u4f1a\u6709<a href=\"https:\/\/docs.python.org\/3\/library\/stdtypes.html#str.title\">\u6807\u9898\u3002<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Submit<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>def main():\n    items = {\n    \"baja taco\": 4.25,\n    \"burrito\": 7.50,\n    \"bowl\": 8.50,\n    \"nachos\": 11.00,\n    \"quesadilla\": 8.50,\n    \"super burrito\": 8.50,\n    \"super quesadilla\": 9.50,\n    \"taco\": 3.00,\n    \"tortilla salad\": 8.00\n    }\n    total = 0\n    while True:\n        try:\n            item = input(\"Item: \").lower()\n            if item in items:\n                total += items&#91;item]\n                print(f\"Total: ${total:.2f}\")\n        except EOFError:\n            print()\n            break\n\n\nmain()\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Grocery List<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Problem<\/h4>\n\n\n\n<p>Suppose that you\u2019re in the habit of making a list of items you need from the grocery store.<br>\u5047\u8bbe\u4f60\u4e60\u60ef\u5217\u51fa\u4ece\u8d85\u5e02\u9700\u8981\u4e70\u7684\u4e1c\u897f\u6e05\u5355\u3002<\/p>\n\n\n\n<p>In a file called&nbsp;<code>grocery.py<\/code>, implement a program that prompts the user for items, one per line, until the user inputs control-d (which is a common way of ending one\u2019s input to a program). Then output the user\u2019s grocery list in all uppercase, sorted alphabetically by item, prefixing each line with the number of times the user inputted that item. No need to pluralize the items. Treat the user\u2019s input case-insensitively.<br>\u5728\u4e00\u4e2a\u540d\u4e3a&nbsp;<code>grocery.py<\/code>&nbsp;\u7684\u6587\u4ef6\u4e2d\uff0c\u5b9e\u73b0\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u6bcf\u884c\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u9879\u76ee\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165 control-d\uff08\u8fd9\u662f\u7ed3\u675f\u7a0b\u5e8f\u8f93\u5165\u7684\u5e38\u89c1\u65b9\u5f0f\uff09\u3002\u7136\u540e\u8f93\u51fa\u7528\u6237\u7684\u8d2d\u7269\u6e05\u5355\uff0c\u5168\u90e8\u5927\u5199\uff0c\u6309\u9879\u76ee\u5b57\u6bcd\u987a\u5e8f\u6392\u5e8f\uff0c\u6bcf\u884c\u524d\u52a0\u4e0a\u7528\u6237\u8f93\u5165\u8be5\u7269\u54c1\u7684\u6b21\u6570\u3002\u6ca1\u5fc5\u8981\u628a\u8fd9\u4e9b\u8bcd\u590d\u6570\u5316\u3002\u5904\u7406\u7528\u6237\u8f93\u5165case-insensitively.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Submit<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>def main():\n    groceries = {}\n    while True:\n        try:\n            item = input().upper()\n            if item in groceries:\n                groceries&#91;item] += 1\n            else:\n                groceries&#91;item] = 1\n        except EOFError:\n            print()\n            for k in sorted(groceries):\n                print(f\"{groceries&#91;k]} {k}\")\n            break\n\n\nmain()\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Outdated<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Problem<\/h4>\n\n\n\n<p>In the United States, dates are typically formatted in&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Date_and_time_notation_in_the_United_States\">month-day-year order<\/a>&nbsp;(MM\/DD\/YYYY), otherwise known as&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Endianness#Middle-endian\">middle-endian<\/a>&nbsp;order, which is arguably bad design. Dates in that format can\u2019t be easily sorted because the date\u2019s year comes last instead of first. Try sorting, for instance,&nbsp;<code>2\/2\/1800<\/code>,&nbsp;<code>3\/3\/1900<\/code>, and&nbsp;<code>1\/1\/2000<\/code>&nbsp;chronologically in any program (e.g., a spreadsheet). Dates in that format are also ambiguous. Harvard was&nbsp;<a href=\"https:\/\/www.harvard.edu\/about\/history\/\">founded<\/a>&nbsp;on September 8, 1636, but 9\/8\/1636 could also be interpreted as August 9, 1636!<br>\u5728\u7f8e\u56fd\uff0c\u65e5\u671f\u901a\u5e38\u6309<a href=\"https:\/\/en.wikipedia.org\/wiki\/Date_and_time_notation_in_the_United_States\">\u6708-\u65e5-\u5e74\u987a\u5e8f&nbsp;<\/a>\uff08MM\/DD\/YYYY\uff09\u683c\u5f0f\u5316\uff0c\u4e5f\u79f0\u4e3a<a href=\"https:\/\/en.wikipedia.org\/wiki\/Endianness#Middle-endian\">\u4e2d\u95f4\u7aef<\/a>\u5e8f\uff0c\u8fd9\u6216\u8bb8\u662f\u8bbe\u8ba1\u4e0d\u4f73\u3002\u8fd9\u79cd\u683c\u5f0f\u7684\u65e5\u671f\u4e0d\u5bb9\u6613\u6392\u5e8f\uff0c\u56e0\u4e3a\u65e5\u671f\u7684\u5e74\u4efd\u6392\u5728\u6700\u540e\u800c\u4e0d\u662f\u7b2c\u4e00\u3002\u6bd4\u5982\uff0c\u5728\u4efb\u4f55\u7a0b\u5e8f\uff08\u6bd4\u5982\u7535\u5b50\u8868\u683c\uff09\u4e2d\uff0c\u5c1d\u8bd5\u6309\u65f6\u95f4\u987a\u5e8f\u6392\u5e8f 2<code>\/2\/1800<\/code>\u30013<code>\/3\/1<\/code>\/<code>1 2000<\/code>\u3002\u8be5\u683c\u5f0f\u7684\u65e5\u671f\u4e5f\u5b58\u5728\u6b67\u4e49\u3002\u54c8<a href=\"https:\/\/www.harvard.edu\/about\/history\/\">\u4f5b\u6210\u7acb\u4e8e&nbsp;<\/a>1636 \u5e74 9 \u6708 8 \u65e5\uff0c\u4f46 9\/8\/1636 \u4e5f\u53ef\u4ee5\u88ab\u89e3\u8bfb\u4e3a 1636 \u5e74 8 \u6708 9 \u65e5\uff01<\/p>\n\n\n\n<p>Fortunately, computers tend to use&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/ISO_8601\">ISO 8601<\/a>, an international standard that prescribes that dates should be formatted in year-month-day (YYYY-MM-DD) order, no matter the country, formatting years with four digits, months with two digits, and days with two digits, \u201cpadding\u201d each with leading zeroes as needed.<br>\u5e78\u8fd0\u7684\u662f\uff0c\u8ba1\u7b97\u673a\u901a\u5e38\u91c7\u7528&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/ISO_8601\">ISO 8601<\/a>\uff0c\u8fd9\u662f\u4e00\u9879\u56fd\u9645\u6807\u51c6\uff0c\u89c4\u5b9a\u65e0\u8bba\u56fd\u5bb6\uff0c\u65e5\u671f\u90fd\u5e94\u6309\u5e74-\u6708-\u65e5\uff08YYYY-MM-DD\uff09\u683c\u5f0f\u5316\uff0c\u5e74\u7528\u56db\u4f4d\u6570\uff0c\u6708\u4efd\u7528\u4e24\u4f4d\u6570\uff0c\u65e5\u7528\u4e24\u4f4d\u6570\u5b57\uff0c\u5e76\u6839\u636e\u9700\u8981\u201c\u586b\u5145\u201d\u6bcf\u4e2a\u65e5\u671f\u7684\u524d\u7f6e\u96f6\u3002<\/p>\n\n\n\n<p>In a file called&nbsp;<code>outdated.py<\/code>, implement a program that prompts the user for a date,&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Anno_Domini\">anno Domini<\/a>, in month-day-year order, formatted like&nbsp;<code>9\/8\/1636<\/code>&nbsp;or&nbsp;<code>September 8, 1636<\/code>, wherein the month in the latter might be any of the values in the&nbsp;<code>list<\/code>&nbsp;below:<br>\u5728\u4e00\u4e2a\u540d\u4e3a&nbsp;<code>outdated.py<\/code>&nbsp;\u7684\u6587\u4ef6\u4e2d\uff0c\u5b9e\u73b0\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u63d0\u793a\u7528\u6237\u6309\u6708-\u65e5-\u5e74\u987a\u5e8f\u8f93\u5165\u65e5\u671f&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Anno_Domini\">anno Domini<\/a>\uff0c\u683c\u5f0f\u5982&nbsp;<code>1636 \u5e74 9 \u6708 8<\/code>&nbsp;\u65e5\u6216&nbsp;<code>1636 \u5e74 9 \u6708 8<\/code>&nbsp;\u65e5\uff0c\u5176\u4e2d\u540e\u8005\u7684\u6708\u4efd\u53ef\u4ee5\u662f\u4ee5\u4e0b<code>\u5217\u8868\u4e2d<\/code>\u7684\u4efb\u4f55\u4e00\u4e2a\u503c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;\n    \"January\",\n    \"February\",\n    \"March\",\n    \"April\",\n    \"May\",\n    \"June\",\n    \"July\",\n    \"August\",\n    \"September\",\n    \"October\",\n    \"November\",\n    \"December\"\n]\n<\/code><\/pre>\n\n\n\n<p>Then output that same date in&nbsp;<code>YYYY-MM-DD<\/code>&nbsp;format. If the user\u2019s input is not a valid date in either format, prompt the user again. Assume that every month has no more than 31 days; no need to validate whether a month has 28, 29, 30, or 31 days.<br>\u7136\u540e\u4ee5&nbsp;<code>YYYY-MM-DD<\/code>&nbsp;\u683c\u5f0f\u8f93\u51fa\u540c\u4e00\u65e5\u671f\u3002\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u65e5\u671f\u5728\u4efb\u4e00\u683c\u5f0f\u4e2d\u90fd\u4e0d\u6709\u6548\uff0c\u8bf7\u518d\u6b21\u63d0\u793a\u8be5\u7528\u6237\u3002\u5047\u8bbe\u6bcf\u4e2a\u6708\u7684\u5929\u6570\u4e0d\u8d85\u8fc7 31 \u5929;\u65e0\u9700\u9a8c\u8bc1\u4e00\u4e2a\u6708\u662f 28 \u5929\u300129 \u5929\u300130 \u5929\u8fd8\u662f 31 \u5929\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Submit<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>def main():\n    months = &#91;\n    \"January\",\n    \"February\",\n    \"March\",\n    \"April\",\n    \"May\",\n    \"June\",\n    \"July\",\n    \"August\",\n    \"September\",\n    \"October\",\n    \"November\",\n    \"December\"\n    ]\n    while True:\n        try:\n            date = input(\"Date: \")\n            if \"\/\" in date:\n                month_str, day_str, year_str = date.split(\"\/\")\n                month = int(month_str)\n                day = int(day_str)\n                year = int(year_str)\n            else:\n                month_str, day_str, year_str = date.split(\" \")\n                month = int(months.index(month_str) + 1)\n                if \",\" not in day_str:\n                    continue\n                else:\n                    day = int(day_str.replace(\",\", \"\"))\n                year = int(year_str)\n            if 1 &lt;= day &lt;= 31 and 1 &lt;= month &lt;= 12:\n                print(f\"{year}-{month:02d}-{day:02d}\")\n                break\n            else:\n                pass\n        except:\n            pass\n\n\n\nmain()\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8bfe\u7a0b\uff1aCS50\u2019s Introduction to Programming with Python CS50  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3507,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[15,32,3],"class_list":["post-3544","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cs50","tag-homework","tag-share","tag-xxbj"],"_links":{"self":[{"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/posts\/3544","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/comments?post=3544"}],"version-history":[{"count":8,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/posts\/3544\/revisions"}],"predecessor-version":[{"id":3552,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/posts\/3544\/revisions\/3552"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/media\/3507"}],"wp:attachment":[{"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/media?parent=3544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/categories?post=3544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/tags?post=3544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}