{"id":3526,"date":"2025-10-20T20:03:27","date_gmt":"2025-10-20T12:03:27","guid":{"rendered":"https:\/\/tobykskgd.life\/?p=3526"},"modified":"2025-10-20T20:03:29","modified_gmt":"2025-10-20T12:03:29","slug":"cs50p-problem-set-1","status":"publish","type":"post","link":"https:\/\/tobykskgd.life\/index.php\/cs50p-problem-set-1\/","title":{"rendered":"CS50P Problem Set 1"},"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\">Deep Thought<\/h3>\n\n\n\n<p>Problem:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u201cAll right,\u201d said the computer, and settled into silence again. The two men fidgeted. The tension was unbearable.\u201c\u597d\u5427\uff0c\u201d\u7535\u8111\u8bf4\uff0c\u7136\u540e\u53c8\u9677\u5165\u4e86\u6c89\u9ed8\u3002\u4e24\u4e2a\u4eba\u5750\u7acb\u4e0d\u5b89\u3002\u7d27\u5f20\u611f\u96be\u4ee5\u5fcd\u53d7\u3002<br>\u201cYou\u2019re really not going to like it,\u201d observed Deep Thought.\u201c\u4f60\u771f\u7684\u4e0d\u4f1a\u559c\u6b22\u5b83\uff0c\u201d\u6df1\u601d\u719f\u8651\u8bf4\u3002<br>\u201cTell us!\u201d&nbsp;&nbsp;\u201c\u544a\u8bc9\u6211\u4eec\uff01\u201d<br>\u201cAll right,\u201d said Deep Thought. \u201cThe Answer to the Great Question\u2026\u201d\u201c\u597d\u5427\uff0c\u201d\u6df1\u601d\u8bf4\u3002\u201c\u8fd9\u4e2a\u4f1f\u5927\u95ee\u9898\u7684\u7b54\u6848&#8230;&#8230;\u201d<br>\u201cYes\u2026!\u201d&nbsp;&nbsp;\u201c\u662f\u7684&#8230;&#8230;\uff01\u201d<br>\u201cOf Life, the Universe and Everything\u2026\u201d said Deep Thought.\u201c\u5173\u4e8e\u751f\u547d\u3001\u5b87\u5b99\u548c\u4e00\u5207&#8230;&#8230;\u201d\u6df1\u601d\u8bf4\u9053\u3002<br>\u201cYes\u2026!\u201d&nbsp;&nbsp;\u201c\u662f\u7684&#8230;&#8230;\uff01\u201d<br>\u201cIs\u2026\u201d said Deep Thought, and paused.\u201c\u662f&#8230;&#8230;\u201d\u6df1\u601d\u8bf4\u9053\uff0c\u505c\u987f\u4e86\u4e00\u4e0b\u3002<br>\u201cYes\u2026!\u201d&nbsp;&nbsp;\u201c\u662f\u7684&#8230;&#8230;\uff01\u201d<br>\u201cIs\u2026\u201d&nbsp;&nbsp;\u201c\u662f&#8230;&#8230;\u201d<br>\u201cYes\u2026!!!\u2026?\u201d&nbsp;&nbsp;\u201c\u662f\u7684&#8230;&#8230;!! \u2026?\u201d<br>\u201cForty-two,\u201d said Deep Thought, with infinite majesty and calm.\u201d\u201c\u56db\u5341\u4e8c\uff0c\u201d\u6df1\u601d\u8bf4\uff0c\u5e26\u7740\u65e0\u9650\u7684\u5a01\u4e25\u548c\u5e73\u9759\u3002<\/p>\n\n\n\n<p>\u2014&nbsp;<em>The Hitchhiker\u2019s Guide to the Galaxy<\/em>, Douglas Adams\u2014 \u94f6<em>\u6cb3\u7cfb\u6f2b\u6e38\u6307\u5357&nbsp;<\/em>\uff0c\u9053\u683c\u62c9\u65af\u00b7\u4e9a\u5f53\u65af<\/p>\n<\/blockquote>\n\n\n\n<p>In&nbsp;<code>deep.py<\/code>, implement a program that prompts the user for the answer to the Great Question of Life, the Universe and Everything, outputting&nbsp;<code>Yes<\/code>&nbsp;if the user inputs&nbsp;<code>42<\/code>&nbsp;or (case-insensitively)&nbsp;<code>forty-two<\/code>&nbsp;or&nbsp;<code>forty two<\/code>. Otherwise output&nbsp;<code>No<\/code>.<br><code>deep.py<\/code>\uff0c\u5b9e\u73b0\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u63d0\u793a\u7528\u6237\u56de\u7b54\u751f\u547d\u3001\u5b87\u5b99\u548c\u4e07\u7269\u7684\u4f1f\u5927\u95ee\u9898\uff0c\u5982\u679c\u7528\u6237\u8f93\u5165&nbsp;<code>42<\/code>&nbsp;\u6216\uff08\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff09<code>&nbsp;\u56db\u5341\u4e8c<\/code>\u6216<code>\u56db\u5341\u4e8c&nbsp;<\/code>\uff0c\u5219\u8f93\u51fa&nbsp;<code>Yes<\/code>\u3002\u5426\u5219\u8f93\u51fa&nbsp;<code>No<\/code>\u3002<\/p>\n\n\n\n<p>Submit:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>answer = input(\"What is the Answer to the Great Question of Life, the Universe, and Everything? \")\n\nif answer.replace(\" \", \"\") == \"42\" or answer.lower() == \"forty-two\" or answer.lower() == \"forty two\":\n    print(\"Yes\")\nelse:\n    print(\"No\")\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Home Federal Savings Bank<\/h3>\n\n\n\n<p>Problem:<\/p>\n\n\n\n<p>In&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/The_Invitations\">season 7, episode 24<\/a>&nbsp;of&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Seinfeld\">Seinfeld<\/a>,&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Cosmo_Kramer\">Kramer<\/a>&nbsp;visits a bank that promises to give $100 to anyone who isn\u2019t greeted with a \u201chello.\u201d Kramer is instead greeted with a \u201chey,\u201d which he insists isn\u2019t a \u201chello,\u201d and so he asks for $100. The bank\u2019s manager proposes a compromise: \u201cYou got a greeting that starts with an \u2018h,\u2019 how does $20 sound?\u201d Kramer accepts.<br>\u5728\u300a<a href=\"https:\/\/en.wikipedia.org\/wiki\/Seinfeld\">&nbsp;\u5b8b\u98de\u6b63\u4f20&nbsp;<\/a>\u300b<a href=\"https:\/\/en.wikipedia.org\/wiki\/The_Invitations\">&nbsp;\u7b2c 7 \u5b63\u7b2c 24 \u96c6\u4e2d&nbsp;<\/a>\uff0c<a href=\"https:\/\/en.wikipedia.org\/wiki\/Cosmo_Kramer\">&nbsp;\u514b\u83b1\u9ed8<\/a>\u62dc\u8bbf\u4e86\u4e00\u5bb6\u94f6\u884c\uff0c\u8be5\u94f6\u884c\u627f\u8bfa\u5411\u4efb\u4f55\u6ca1\u6709\u6253\u62db\u547c\u201c\u4f60\u597d\u201d\u7684\u4eba\u63d0\u4f9b 100 \u7f8e\u5143\u3002\u76f8\u53cd\uff0c\u514b\u83b1\u9ed8\u53d7\u5230\u4e86\u201c\u563f\u201d\u7684\u6b22\u8fce\uff0c\u4ed6\u575a\u79f0\u8fd9\u4e0d\u662f\u201c\u4f60\u597d\u201d\uff0c\u6240\u4ee5\u4ed6\u8981\u4e86 100 \u7f8e\u5143\u3002\u94f6\u884c\u7ecf\u7406\u63d0\u51fa\u4e86\u4e00\u4e2a\u6298\u8877\u65b9\u6848\uff1a\u201c\u4f60\u6709\u4e00\u4e2a\u4ee5&#8217;h&#8217;\u5f00\u5934\u7684\u95ee\u5019\u8bed\uff0c20 \u7f8e\u5143\u542c\u8d77\u6765\u600e\u4e48\u6837\uff1f\u514b\u83b1\u9ed8\u63a5\u53d7\u4e86\u3002<\/p>\n\n\n\n<p>In a file called&nbsp;<code>bank.py<\/code>, implement a program that prompts the user for a greeting. If the greeting starts with \u201chello\u201d, output&nbsp;<code>$0<\/code>. If the greeting starts with an \u201ch\u201d (but not \u201chello\u201d), output&nbsp;<code>$20<\/code>. Otherwise, output&nbsp;<code>$100<\/code>. Ignore any leading whitespace in the user\u2019s greeting, and treat the user\u2019s greeting case-insensitively.<br>\u5728\u540d\u4e3a&nbsp;<code>bank.py<\/code>&nbsp;\u7684\u6587\u4ef6\u4e2d\uff0c\u5b9e\u73b0\u4e00\u4e2a\u63d0\u793a\u7528\u6237\u6253\u95ee\u5019\u8bed\u7684\u7a0b\u5e8f\u3002\u5982\u679c\u95ee\u5019\u8bed\u4ee5\u201chello\u201d\u5f00\u5934\uff0c\u5219\u8f93\u51fa&nbsp;<code>$0<\/code>\u3002\u5982\u679c\u95ee\u5019\u8bed\u4ee5\u201ch\u201d\u5f00\u5934\uff08\u4f46\u4e0d\u662f\u201chello\u201d\uff09\uff0c\u5219\u8f93\u51fa&nbsp;<code>$20<\/code>\u3002\u5426\u5219\uff0c\u8f93\u51fa&nbsp;<code>100 \u7f8e\u5143&nbsp;<\/code>\u3002\u5ffd\u7565\u7528\u6237\u95ee\u5019\u8bed\u4e2d\u7684\u4efb\u4f55\u524d\u5bfc\u7a7a\u683c\uff0c\u5e76\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u6765\u5904\u7406\u7528\u6237\u7684\u95ee\u5019\u8bed\u3002<\/p>\n\n\n\n<p>Submit:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>say = input(\"Greeting: \")\n\nif say.lower().strip()&#91;:5] == \"hello\":\n    print(\"$0\")\nelif say.lower().strip()&#91;:1] == \"h\":\n    print(\"$20\")\nelse:\n    print(\"$100\")\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">File Extensions<\/h3>\n\n\n\n<p>Problem:<\/p>\n\n\n\n<p>Even though Windows and macOS sometimes hide them, most files have&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Filename_extension\">file extensions<\/a>, a suffix that starts with a period (<code>.<\/code>) at the end of their name. For instance, file names for&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/GIF\">GIFs<\/a>&nbsp;end with&nbsp;<code>.gif<\/code>, and file names for&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/JPEG\">JPEGs<\/a>&nbsp;end with&nbsp;<code>.jpg<\/code>&nbsp;or&nbsp;<code>.jpeg<\/code>. When you double-click on a file to open it, your computer uses its file extension to determine which program to launch.<br>\u5c3d\u7ba1 Windows \u548c macOS \u6709\u65f6\u4f1a\u9690\u85cf\u5b83\u4eec\uff0c\u4f46\u5927\u591a\u6570\u6587\u4ef6\u90fd\u6709<a href=\"https:\/\/en.wikipedia.org\/wiki\/Filename_extension\">\u6587\u4ef6\u6269\u5c55\u540d&nbsp;<\/a>\uff0c\u5373\u540d\u79f0\u672b\u5c3e\u4ee5\u53e5\u70b9 \uff08<code>.\uff09<\/code>&nbsp;\u5f00\u5934\u7684\u540e\u7f00\u3002\u4f8b\u5982\uff0c<a href=\"https:\/\/en.wikipedia.org\/wiki\/GIF\">GIF<\/a>&nbsp;\u7684\u6587\u4ef6\u540d\u4ee5&nbsp;<code>.gif<\/code>&nbsp;\u7ed3\u5c3e\uff0c<a href=\"https:\/\/en.wikipedia.org\/wiki\/JPEG\">JPEG<\/a>&nbsp;\u7684\u6587\u4ef6\u540d\u4ee5&nbsp;<code>.jpg<\/code>&nbsp;\u6216&nbsp;<code>.jpeg<\/code>&nbsp;\u7ed3\u5c3e\u3002\u5f53\u60a8\u53cc\u51fb\u6587\u4ef6\u4ee5\u6253\u5f00\u5b83\u65f6\uff0c\u60a8\u7684\u8ba1\u7b97\u673a\u4f1a\u4f7f\u7528\u5176\u6587\u4ef6\u6269\u5c55\u540d\u6765\u786e\u5b9a\u8981\u542f\u52a8\u7684\u7a0b\u5e8f\u3002<\/p>\n\n\n\n<p>Web browsers, by contrast, rely on&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Media_type\">media types<\/a>, formerly known as MIME types, to determine how to display files that live on the web. When you download a file from a web server, that server sends an&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/List_of_HTTP_header_fields\">HTTP header<\/a>, along with the file itself, indicating the file\u2019s media type. For instance, the media type for a GIF is&nbsp;<code>image\/gif<\/code>, and the media type for a JPEG is&nbsp;<code>image\/jpeg<\/code>. To determine the media type for a file, a web server typically looks at the file\u2019s extension, mapping one to the other.<br>\u76f8\u6bd4\u4e4b\u4e0b\uff0cWeb \u6d4f\u89c8\u5668\u4f9d\u8d56\u4e8e<a href=\"https:\/\/en.wikipedia.org\/wiki\/Media_type\">\u5a92\u4f53\u7c7b\u578b&nbsp;<\/a>\uff08\u4ee5\u524d\u79f0\u4e3a MIME \u7c7b\u578b\uff09\u6765\u786e\u5b9a\u5982\u4f55\u663e\u793a Web \u4e0a\u7684\u6587\u4ef6\u3002\u5f53\u60a8\u4ece Web \u670d\u52a1\u5668\u4e0b\u8f7d\u6587\u4ef6\u65f6\uff0c\u8be5\u670d\u52a1\u5668\u4f1a\u53d1\u9001\u4e00\u4e2a&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/List_of_HTTP_header_fields\">HTTP \u6807\u5934<\/a>\u4ee5\u53ca\u6587\u4ef6\u672c\u8eab\uff0c\u6307\u793a\u6587\u4ef6\u7684\u5a92\u4f53\u7c7b\u578b\u3002\u4f8b\u5982\uff0cGIF \u7684\u5a92\u4f53\u7c7b\u578b\u662f&nbsp;<code>image\/gif\uff0cJPEG<\/code>&nbsp;\u7684\u5a92\u4f53\u7c7b\u578b\u662f&nbsp;<code>image\/jpeg<\/code>\u3002\u4e3a\u4e86\u786e\u5b9a\u6587\u4ef6\u7684\u5a92\u4f53\u7c7b\u578b\uff0cWeb \u670d\u52a1\u5668\u901a\u5e38\u4f1a\u67e5\u770b\u6587\u4ef6\u7684\u6269\u5c55\u540d\uff0c\u5c06\u4e00\u4e2a\u6269\u5c55\u540d\u6620\u5c04\u5230\u53e6\u4e00\u4e2a\u6269\u5c55\u540d\u3002<\/p>\n\n\n\n<p>See&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Basics_of_HTTP\/MIME_types\/Common_types\">developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Basics_of_HTTP\/MIME_types\/Common_types<\/a>&nbsp;for common types.<br>\u5e38\u89c1\u7c7b\u578b\u8bf7\u53c2\u89c1&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Basics_of_HTTP\/MIME_types\/Common_types\">developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Basics_of_HTTP\/MIME_types\/Common_types<\/a>\u3002<\/p>\n\n\n\n<p>In a file called&nbsp;<code>extensions.py<\/code>, implement a program that prompts the user for the name of a file and then outputs that file\u2019s media type if the file\u2019s name ends, case-insensitively, in any of these suffixes:<br>\u5728\u540d\u4e3a&nbsp;<code>extensions.py<\/code>&nbsp;\u7684\u6587\u4ef6\u4e2d\uff0c\u5b9e\u73b0\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u8be5\u7a0b\u5e8f\u63d0\u793a\u7528\u6237\u8f93\u5165\u6587\u4ef6\u540d\uff0c\u7136\u540e\u5728\u6587\u4ef6\u540d\u4ee5\u4ee5\u4e0b\u4efb\u4f55\u540e\u7f00\u7ed3\u5c3e\u65f6\u8f93\u51fa\u8be5\u6587\u4ef6\u7684\u5a92\u4f53\u7c7b\u578b\uff0c\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>.gif<\/code><\/li>\n\n\n\n<li><code>.jpg<\/code><\/li>\n\n\n\n<li><code>.jpeg<\/code><\/li>\n\n\n\n<li><code>.png<\/code><\/li>\n\n\n\n<li><code>.pdf<\/code><\/li>\n\n\n\n<li><code>.txt<\/code><\/li>\n\n\n\n<li><code>.zip<\/code><\/li>\n<\/ul>\n\n\n\n<p>If the file\u2019s name ends with some other suffix or has no suffix at all, output&nbsp;<code>application\/octet-stream<\/code>&nbsp;instead, which is a common default.<br>\u5982\u679c\u6587\u4ef6\u540d\u4ee5\u5176\u4ed6\u540e\u7f00\u7ed3\u5c3e\u6216\u6839\u672c\u6ca1\u6709\u540e\u7f00\uff0c\u5219\u8f93\u51fa&nbsp;<code>application\/octet-stream<\/code>\uff0c\u8fd9\u662f\u5e38\u89c1\u7684\u9ed8\u8ba4\u503c\u3002<\/p>\n\n\n\n<p>Submit:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>file = input(\"File name: \").lower().strip()\n\next = file.split(\".\")&#91;-1]\n\nmatch ext:\n    case \"gif\":\n        print(\"image\/gif\")\n    case \"jpg\":\n        print(\"image\/jpeg\")\n    case \"jpeg\":\n        print(\"image\/jpeg\")\n    case \"png\":\n        print(\"image\/png\")\n    case \"pdf\":\n        print(\"application\/pdf\")\n    case \"txt\":\n        print(\"text\/plain\")\n    case \"zip\":\n        print(\"application\/zip\")\n    case _:\n        print(\"application\/octet-stream\")\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Math Interpreter<\/h3>\n\n\n\n<p>Problem:<\/p>\n\n\n\n<p>Python already supports math, whereby&nbsp;<em>you<\/em>&nbsp;can write code to add, subtract, multiply, or divide values and even variables. But let\u2019s write a program that enables&nbsp;<em>users<\/em>&nbsp;to do math, even without knowing Python.<br>Python \u5df2\u7ecf\u652f\u6301\u6570\u5b66\uff0c<em>&nbsp;\u60a8\u53ef\u4ee5<\/em>\u7f16\u5199\u4ee3\u7801\u6765\u52a0\u3001\u51cf\u3001\u4e58\u6216\u9664\u503c\u751a\u81f3\u53d8\u91cf\u3002\u4f46\u662f\u8ba9\u6211\u4eec\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u4f7f<em>\u7528\u6237<\/em>\u80fd\u591f\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\uff0c\u5373\u4f7f\u4e0d\u61c2 Python\u3002<\/p>\n\n\n\n<p>In a file called&nbsp;<code>interpreter.py<\/code>, implement a program that prompts the user for an arithmetic expression and then calculates and outputs the result as a floating-point value formatted to one decimal place. Assume that the user\u2019s input will be formatted as&nbsp;<code>x y z<\/code>, with one space between&nbsp;<code>x<\/code>&nbsp;and&nbsp;<code>y<\/code>&nbsp;and one space between&nbsp;<code>y<\/code>&nbsp;and&nbsp;<code>z<\/code>, wherein:<br>\u5728\u540d\u4e3a&nbsp;<code>interpreter.py<\/code>&nbsp;\u7684\u6587\u4ef6\u4e2d\uff0c\u5b9e\u73b0\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u8be5\u7a0b\u5e8f\u63d0\u793a\u7528\u6237\u8f93\u5165\u7b97\u672f\u8868\u8fbe\u5f0f\uff0c\u7136\u540e\u8ba1\u7b97\u7ed3\u679c\u5e76\u5c06\u5176\u8f93\u51fa\u4e3a\u683c\u5f0f\u5316\u4e3a\u5c0f\u6570\u70b9\u540e\u4e00\u4f4d\u7684\u6d6e\u70b9\u503c\u3002\u5047\u8bbe\u7528\u6237\u7684\u8f93\u5165\u5c06\u683c\u5f0f\u5316\u4e3a&nbsp;<code>x y z\uff0cx<\/code>&nbsp;\u548c&nbsp;<code>y<\/code>&nbsp;\u4e4b\u95f4\u6709\u4e00\u4e2a\u7a7a\u683c\uff0c<code>y<\/code>&nbsp;\u548c&nbsp;<code>z<\/code>&nbsp;\u4e4b\u95f4<code><\/code>\u6709\u4e00\u4e2a\u7a7a\u683c\uff0c\u5176\u4e2d\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>x<\/code>\u00a0is an integer\u00a0\u00a0<code>x<\/code>\u00a0\u662f\u4e00\u4e2a\u6574\u6570<\/li>\n\n\n\n<li><code>y<\/code>\u00a0is\u00a0<code>+<\/code>,\u00a0<code>-<\/code>,\u00a0<code>*<\/code>, or\u00a0<code>\/<\/code><br><code>y<\/code>\u00a0\u662f\u00a0<code>+<\/code>\u3001<code>-<\/code>\u3001<code>*<\/code>\u00a0\u6216\u00a0<code>\/<\/code><\/li>\n\n\n\n<li><code>z<\/code>\u00a0is an integer\u00a0\u00a0<code>z<\/code>\u00a0\u662f\u4e00\u4e2a\u6574\u6570<\/li>\n<\/ul>\n\n\n\n<p>For instance, if the user inputs&nbsp;<code>1 + 1<\/code>, your program should output&nbsp;<code>2.0<\/code>. Assume that, if&nbsp;<code>y<\/code>&nbsp;is&nbsp;<code>\/<\/code>, then&nbsp;<code>z<\/code>&nbsp;will not be&nbsp;<code>0<\/code>.<br>\u4f8b\u5982\uff0c\u5982\u679c\u7528\u6237\u8f93\u5165&nbsp;<code>1 + 1<\/code>\uff0c\u5219\u7a0b\u5e8f\u5e94\u8f93\u51fa&nbsp;<code>2.0<\/code>\u3002\u5047\u8bbe\uff0c\u5982\u679c&nbsp;<code>y<\/code>&nbsp;\u662f&nbsp;<code>\/<\/code>\uff0c\u5219&nbsp;<code>z<\/code>&nbsp;\u5c06\u4e0d\u662f&nbsp;<code>0<\/code>\u3002<\/p>\n\n\n\n<p>Note that, just as&nbsp;<code>python<\/code>&nbsp;itself is an interpreter for Python, so will your&nbsp;<code>interpreter.py<\/code>&nbsp;be an interpreter for math!<br>\u8bf7\u6ce8\u610f\uff0c\u6b63\u5982&nbsp;<code>python<\/code>&nbsp;\u672c\u8eab\u662f Python \u7684\u89e3\u91ca\u5668\u4e00\u6837\uff0c\u60a8\u7684&nbsp;<code>interpreter.py<\/code>&nbsp;\u4e5f\u4f1a\u6210\u4e3a\u6570\u5b66\u7684\u89e3\u91ca\u5668\uff01<\/p>\n\n\n\n<p>Submit:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>arithmetic = input(\"Expression: \")\n\nx = int(arithmetic.split(\" \")&#91;0])\ny = arithmetic.split(\" \")&#91;1]\nz = int(arithmetic.split(\" \")&#91;-1])\n\nmatch y:\n    case \"+\":\n        print(f\"{x + z:.1f}\")\n    case \"-\":\n        print(f\"{x - z:.1f}\")\n    case \"*\":\n        print(f\"{x * z:.1f}\")\n    case \"\/\":\n        print(f\"{x \/ z:.1f}\")\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Meal Time<\/h3>\n\n\n\n<p>Problem:<\/p>\n\n\n\n<p>Suppose that you\u2019re in a country where it\u2019s customary to eat breakfast between 7:00 and 8:00, lunch between 12:00 and 13:00, and dinner between 18:00 and 19:00. Wouldn\u2019t it be nice if you had a program that could tell you what to eat when?<br>\u5047\u8bbe\u60a8\u6240\u5728\u7684\u56fd\u5bb6\/\u5730\u533a\u4e60\u60ef\u5728 7\uff1a00 \u81f3 8\uff1a00 \u4e4b\u95f4\u5403\u65e9\u9910\uff0c\u5728 12\uff1a00 \u81f3 13\uff1a00 \u4e4b\u95f4\u5403\u5348\u9910\uff0c\u5728 18\uff1a00 \u81f3 19\uff1a00 \u4e4b\u95f4\u5403\u665a\u9910\u3002\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u7a0b\u5e8f\u53ef\u4ee5\u544a\u8bc9\u4f60\u4ec0\u4e48\u65f6\u5019\u5403\u4ec0\u4e48\u4e0d\u662f\u5f88\u597d\u5417\uff1f<\/p>\n\n\n\n<p>In&nbsp;<code>meal.py<\/code>, implement a program that prompts the user for a time and outputs whether it\u2019s&nbsp;<code>breakfast time<\/code>,&nbsp;<code>lunch time<\/code>, or&nbsp;<code>dinner time<\/code>. If it\u2019s not time for a meal, don\u2019t output anything at all. Assume that the user\u2019s input will be formatted in 24-hour time as&nbsp;<code>#:##<\/code>&nbsp;or&nbsp;<code>##:##<\/code>. And assume that each meal\u2019s time range is inclusive. For instance, whether it\u2019s 7:00, 7:01, 7:59, or 8:00, or anytime in between, it\u2019s time for breakfast.<br>\u5728&nbsp;<code>meal.py<\/code>&nbsp;\u4e2d\uff0c\u5b9e\u73b0\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u63d0\u793a\u7528\u6237\u4e00\u6bb5\u65f6\u95f4\u5e76\u8f93\u51fa\u662f<code>\u65e9\u9910\u65f6\u95f4&nbsp;<\/code>\u3001<code>&nbsp;\u5348\u9910\u65f6\u95f4<\/code>\u8fd8\u662f<code>\u665a\u9910\u65f6\u95f4&nbsp;<\/code>\u3002\u5982\u679c\u4e0d\u662f\u5403\u996d\u7684\u65f6\u95f4\uff0c\u5c31\u4ec0\u4e48\u90fd\u4e0d\u8981\u8f93\u51fa\u3002\u5047\u8bbe\u7528\u6237\u7684\u8f93\u5165\u5c06\u5728 24 \u5c0f\u65f6\u5185\u683c\u5f0f\u5316\u4e3a&nbsp;<code>#\uff1a##<\/code>&nbsp;\u6216&nbsp;<code>##\uff1a##<\/code>\u3002\u5e76\u5047\u8bbe\u6bcf\u987f\u996d\u7684\u65f6\u95f4\u8303\u56f4\u662f\u5305\u5bb9\u6027\u7684\u3002\u4f8b\u5982\uff0c\u65e0\u8bba\u662f 7\uff1a00\u30017\uff1a01\u30017\uff1a59 \u8fd8\u662f 8\uff1a00\uff0c\u8fd8\u662f\u4ecb\u4e8e\u4e24\u8005\u4e4b\u95f4\u7684\u4efb\u4f55\u65f6\u95f4\uff0c\u90fd\u662f\u5403\u65e9\u9910\u7684\u65f6\u95f4\u3002<\/p>\n\n\n\n<p>Structure your program per the below, wherein&nbsp;<code>convert<\/code>&nbsp;is a function (that can be called by&nbsp;<code>main<\/code>) that converts&nbsp;<code>time<\/code>, a&nbsp;<code>str<\/code>&nbsp;in 24-hour format, to the corresponding number of hours as a&nbsp;<code>float<\/code>. For instance, given a&nbsp;<code>time<\/code>&nbsp;like&nbsp;<code>\"7:30\"<\/code>&nbsp;(i.e., 7 hours and 30 minutes),&nbsp;<code>convert<\/code>&nbsp;should return&nbsp;<code>7.5<\/code>&nbsp;(i.e., 7.5 hours).<br>\u6309\u7167\u4ee5\u4e0b\u65b9\u5f0f\u6784\u5efa\u60a8\u7684\u7a0b\u5e8f\uff0c\u5176\u4e2d&nbsp;<code>convert<\/code>&nbsp;\u662f\u4e00\u4e2a\u51fd\u6570\uff08\u53ef\u4ee5\u7531&nbsp;<code>main<\/code>&nbsp;\u8c03\u7528\uff09\uff0c\u5b83\u5c06<code>\u65f6\u95f4&nbsp;<\/code>\uff0824 \u5c0f\u65f6\u683c\u5f0f\u7684&nbsp;<code>str<\/code>\uff09\u8f6c\u6362\u4e3a\u76f8\u5e94\u7684\u6d6e<code>\u70b9<\/code>\u6570\u5c0f\u65f6\u6570\u3002\u4f8b\u5982\uff0c\u7ed9\u5b9a\u50cf&nbsp;<code>\u201c7\uff1a30\u201d<\/code>&nbsp;\u8fd9\u6837\u7684<code>\u65f6\u95f4&nbsp;<\/code>\uff08\u5373 7 \u5c0f\u65f6 30 \u5206\u949f\uff09\uff0c<code>convert<\/code>&nbsp;\u5e94\u8be5\u8fd4\u56de&nbsp;<code>7.5<\/code>\uff08\u5373 7.5 \u5c0f\u65f6\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>def<\/strong> main():\n    ...\n\n\n<strong>def<\/strong> convert(time):\n    ...\n\n\n<strong>if<\/strong> __name__ == \"__main__\":\n    main()<\/code><\/pre>\n\n\n\n<p>Submit:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def main():\n    now = convert(input(\"What time is it? \"))\n    if 7 &lt;= now &lt;= 8:\n        print(\"breakfast time\")\n    elif 12 &lt;= now &lt;= 13:\n        print(\"lunch time\")\n    elif 18 &lt;= now &lt;= 19:\n        print(\"dinner time\")\n\n\ndef convert(time):\n    h = float(time.split(\":\")&#91;0])\n    m = float(time.split(\":\")&#91;-1]) \/ 60\n    return h + m\n\n\nif __name__ == \"__main__\":\n    main()\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Challenge<\/h4>\n\n\n\n<p>If up for a challenge, optionally add support for 12-hour times, allowing the user to input times in these formats too:<br>\u5982\u679c\u9700\u8981\u6311\u6218\uff0c\u53ef\u4ee5\u9009\u62e9\u6dfb\u52a0\u5bf9 12 \u5c0f\u65f6\u65f6\u95f4\u7684\u652f\u6301\uff0c\u5141\u8bb8\u7528\u6237\u4e5f\u4ee5\u4ee5\u4e0b\u683c\u5f0f\u8f93\u5165\u65f6\u95f4\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>#:## a.m.<\/code>\u00a0and\u00a0<code>##:## a.m.<\/code><br><code>#\uff1a## \u4e0a\u5348<\/code>\u548c\u00a0<code>##\uff1a## \u4e0a\u5348<\/code><\/li>\n\n\n\n<li><code>#:## p.m.<\/code>\u00a0and\u00a0<code>##:## p.m.<\/code><br><code>#\uff1a## \u4e0b\u5348<\/code>\u548c\u00a0<code>##\uff1a## \u4e0b\u5348<\/code><\/li>\n<\/ul>\n\n\n\n<p>Submit:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def main():\n    now = convert(input(\"What time is it? \"))\n    if 7 &lt;= now &lt;= 8:\n        print(\"breakfast time\")\n    elif 12 &lt;= now &lt;= 13:\n        print(\"lunch time\")\n    elif 18 &lt;= now &lt;= 19:\n        print(\"dinner time\")\n\n\ndef convert(time):\n    time_T = time.replace(\"a.m.\", \"\").replace(\"p.m.\", \"\").strip()\n    h, m = time_T.split(\":\")\n    h = float(h)\n    m = float(m) \/ 60\n\n    if \"p.m.\" in time and h != 12:\n        h += 12\n    elif \"a.m.\" in time and h == 12:\n        h = 0\n\n    return h + m\n\n\nif __name__ == \"__main__\":\n    main()\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-3526","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\/3526","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=3526"}],"version-history":[{"count":6,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/posts\/3526\/revisions"}],"predecessor-version":[{"id":3532,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/posts\/3526\/revisions\/3532"}],"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=3526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/categories?post=3526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/tags?post=3526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}