{"id":3538,"date":"2025-11-16T18:25:25","date_gmt":"2025-11-16T10:25:25","guid":{"rendered":"https:\/\/tobykskgd.life\/?p=3538"},"modified":"2025-11-16T18:25:40","modified_gmt":"2025-11-16T10:25:40","slug":"119","status":"publish","type":"post","link":"https:\/\/tobykskgd.life\/index.php\/119\/","title":{"rendered":"CS50P Problem Set 2"},"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\">camelCase<\/h3>\n\n\n\n<p>Problem:<\/p>\n\n\n\n<p>Source:&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Camel_case\">en.wikipedia.org\/wiki\/Camel_case<\/a><br>\u6765\u6e90\uff1a<a href=\"https:\/\/en.wikipedia.org\/wiki\/Camel_case\">en.wikipedia.org\/wiki\/Camel_case<\/a><\/p>\n\n\n\n<p>In some languages, it\u2019s common to use&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Camel_case\">camel case<\/a>&nbsp;(otherwise known as \u201cmixed case\u201d) for variables\u2019 names when those names comprise multiple words, whereby the first letter of the first word is lowercase but the first letter of each subsequent word is uppercase. For instance, whereas a variable for a user\u2019s name might be called&nbsp;<code>name<\/code>, a variable for a user\u2019s first name might be called&nbsp;<code>firstName<\/code>, and a variable for a user\u2019s preferred first name (e.g., nickname) might be called&nbsp;<code>preferredFirstName<\/code>.<br>\u5728\u67d0\u4e9b\u8bed\u8a00\u4e2d\uff0c\u5f53\u53d8\u91cf\u540d\u79f0\u5305\u542b\u591a\u4e2a\u5355\u8bcd\u65f6\uff0c\u901a\u5e38\u4f7f\u7528<a href=\"https:\/\/en.wikipedia.org\/wiki\/Camel_case\">\u9a7c\u5cf0\u547d\u540d<\/a>\u6cd5\uff08\u4e5f\u79f0\u4e3a\u201c\u6df7\u5408\u5927\u5c0f\u5199\u201d\uff09\u4f5c\u4e3a\u53d8\u91cf\u540d\u79f0\uff0c\u5176\u4e2d\u7b2c\u4e00\u4e2a\u5355\u8bcd\u7684\u7b2c\u4e00\u4e2a\u5b57\u6bcd\u662f\u5c0f\u5199\u7684\uff0c\u4f46\u6bcf\u4e2a\u540e\u7eed\u5355\u8bcd\u7684\u7b2c\u4e00\u4e2a\u5b57\u6bcd\u662f\u5927\u5199\u7684\u3002\u4f8b\u5982\uff0c\u7528\u6237\u540d\u7684\u53d8\u91cf\u53ef\u80fd\u79f0\u4e3a&nbsp;<code>name<\/code>\uff0c\u800c\u7528\u6237\u540d\u5b57\u7684\u53d8\u91cf\u53ef\u80fd\u79f0\u4e3a&nbsp;<code>firstName<\/code>\uff0c\u800c\u7528\u6237\u9996\u9009\u540d\u5b57\uff08\u4f8b\u5982\u6635\u79f0\uff09\u7684\u53d8\u91cf\u53ef\u80fd\u79f0\u4e3a&nbsp;<code>preferredFirstName<\/code>\u3002<\/p>\n\n\n\n<p>Python, by contrast,&nbsp;<a href=\"https:\/\/peps.python.org\/pep-0008\/#function-and-variable-names\">recommends<\/a>&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Snake_case\">snake case<\/a>, whereby words are instead separated by underscores (<code>_<\/code>), with all letters in lowercase. For instance, those same variables would be called&nbsp;<code>name<\/code>,&nbsp;<code>first_name<\/code>, and&nbsp;<code>preferred_first_name<\/code>, respectively, in Python.<br>\u76f8\u6bd4\u4e4b\u4e0b\uff0cPython&nbsp;<a href=\"https:\/\/peps.python.org\/pep-0008\/#function-and-variable-names\">\u5efa\u8bae\u4f7f\u7528<\/a><a href=\"https:\/\/en.wikipedia.org\/wiki\/Snake_case\">\u86c7\u5f62\u5927\u5c0f\u5199&nbsp;<\/a>\uff0c\u5176\u4e2d\u5355\u8bcd\u7528\u4e0b\u5212\u7ebf \uff08<code>_<\/code>\uff09 \u5206\u9694\uff0c\u6240\u6709\u5b57\u6bcd\u5747\u4e3a\u5c0f\u5199\u3002\u4f8b\u5982\uff0c\u8fd9\u4e9b\u76f8\u540c\u7684\u53d8\u91cf\u5728 Python \u4e2d\u5206\u522b\u79f0\u4e3a&nbsp;<code>name<\/code>\u3001<code>first_name<\/code>&nbsp;\u548c&nbsp;<code>preferred_first_name<\/code>\u3002<\/p>\n\n\n\n<p>In a file called&nbsp;<code>camel.py<\/code>, implement a program that prompts the user for the name of a variable in camel case and outputs the corresponding name in snake case. Assume that the user\u2019s input will indeed be in camel case.<br>\u5728\u540d\u4e3a&nbsp;<code>camel.py<\/code>&nbsp;\u7684\u6587\u4ef6\u4e2d\uff0c\u5b9e\u73b0\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u8be5\u7a0b\u5e8f\u63d0\u793a\u7528\u6237\u8f93\u5165\u9a7c\u5cf0\u547d\u540d\u6cd5\u7684\u53d8\u91cf\u540d\u79f0\uff0c\u5e76\u4ee5\u86c7\u5f62\u547d\u540d\u6cd5\u8f93\u51fa\u76f8\u5e94\u7684\u540d\u79f0\u3002\u5047\u8bbe\u7528\u6237\u7684\u8f93\u5165\u786e\u5b9e\u662f\u9a7c\u5cf0\u5f0f\u5927\u5c0f\u5199\u3002<\/p>\n\n\n\n<p>Submit:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>camelCase = input(\"camelCase: \")\nsnake_case = \"\"\n\nfor i in camelCase:\n    if i == i.lower():\n        snake_case += i\n    else:\n        snake_case =  snake_case + '_' + i.lower()\n\nprint(f\"snake_case: {snake_case}\")\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Coke Machine<\/h3>\n\n\n\n<p>problem:<\/p>\n\n\n\n<p>Suppose that a machine sells bottles of Coca-Cola (Coke) for 50 cents and only accepts coins in these denominations: 25 cents, 10 cents, and 5 cents.<br>\u5047\u8bbe\u4e00\u53f0\u673a\u5668\u4ee5 50 \u7f8e\u5206\u7684\u4ef7\u683c\u51fa\u552e\u74f6\u88c5\u53ef\u53e3\u53ef\u4e50\uff08\u53ef\u4e50\uff09\uff0c\u5e76\u4e14\u53ea\u63a5\u53d7\u4ee5\u4e0b\u9762\u989d\u7684\u786c\u5e01\uff1a25 \u7f8e\u5206\u300110 \u7f8e\u5206\u548c 5 \u7f8e\u5206\u3002<\/p>\n\n\n\n<p>In a file called&nbsp;<code>coke.py<\/code>, implement a program that prompts the user to insert a coin, one at a time, each time informing the user of the amount due. Once the user has inputted at least 50 cents, output how many cents in change the user is owed. Assume that the user will only input integers, and ignore any integer that isn\u2019t an accepted denomination.<br>\u5728\u540d\u4e3a&nbsp;<code>coke.py<\/code>&nbsp;\u7684\u6587\u4ef6\u4e2d\uff0c\u5b9e\u73b0\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u63d0\u793a\u7528\u6237\u4e00\u6b21\u63d2\u5165\u4e00\u4e2a\u786c\u5e01\uff0c\u6bcf\u6b21\u90fd\u901a\u77e5\u7528\u6237\u5e94\u4ed8\u91d1\u989d\u3002\u4e00\u65e6\u7528\u6237\u8f93\u5165\u4e86\u81f3\u5c11 50 \u7f8e\u5206\uff0c\u8f93\u51fa\u7528\u6237\u6b20\u591a\u5c11\u96f6\u94b1\u3002\u5047\u8bbe\u7528\u6237\u53ea\u8f93\u5165\u6574\u6570\uff0c\u5e76\u5ffd\u7565\u4efb\u4f55\u4e0d\u63a5\u53d7\u9762\u989d\u7684\u6574\u6570\u3002<\/p>\n\n\n\n<p>Submit:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Amount_Due = 50\nprint(f\"Amount Due: {Amount_Due}\")\n\nwhile Amount_Due > 0:\n    Insert_Coin = int(input(\"Insert Coin: \"))\n    if Insert_Coin == 25 or Insert_Coin == 10 or Insert_Coin == 5:\n        Amount_Due -= Insert_Coin\n        if Amount_Due > 0:\n            print(f\"Amount Due: {Amount_Due}\")\n    else:\n        print(f\"Amount Due: {Amount_Due}\")\n\nprint(f\"Change Owed: {abs(Amount_Due)}\")\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Just setting up my twttr<\/h3>\n\n\n\n<p>problem:<\/p>\n\n\n\n<p>When texting or tweeting, it\u2019s not uncommon to shorten words to save time or space, as by omitting vowels, much like Twitter was originally called&nbsp;<em>twttr<\/em>. In a file called&nbsp;<code>twttr.py<\/code>, implement a program that prompts the user for a&nbsp;<code>str<\/code>&nbsp;of text and then outputs that same text but with all vowels (A, E, I, O, and U) omitted, whether inputted in uppercase or lowercase.<br>\u53d1\u77ed\u4fe1\u6216\u53d1\u63a8\u6587\u65f6\uff0c\u7f29\u77ed\u5355\u8bcd\u4ee5\u8282\u7701\u65f6\u95f4\u6216\u7a7a\u95f4\u7684\u60c5\u51b5\u5e76\u4e0d\u5c11\u89c1\uff0c\u4f8b\u5982\u7701\u7565\u5143\u97f3\uff0c\u5c31\u50cf Twitter \u6700\u521d\u79f0\u4e3a&nbsp;<em>twttr<\/em>&nbsp;\u4e00\u6837\u3002\u5728\u540d\u4e3a&nbsp;<code>twttr.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\u672c&nbsp;<code>str<\/code>\uff0c\u7136\u540e\u8f93\u51fa\u76f8\u540c\u7684\u6587\u672c\uff0c\u4f46\u7701\u7565\u6240\u6709\u5143\u97f3\uff08A\u3001E\u3001I\u3001O \u548c U\uff09\uff0c\u65e0\u8bba\u662f\u4ee5\u5927\u5199\u8fd8\u662f\u5c0f\u5199\u8f93\u5165\u3002<\/p>\n\n\n\n<p>Submit:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>in_put = input(\"Input: \")\nout_put = \"\"\n\nfor i in in_put:\n    if i.lower() != 'a' and i.lower() != 'e' and i.lower() != 'i' and i.lower() != 'o' and i.lower() != 'u':\n        out_put += i\n\nprint(f\"Output: {out_put}\")\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Vanity Plates<\/h3>\n\n\n\n<p>problem:<\/p>\n\n\n\n<p>In Massachusetts, home to Harvard University, it\u2019s possible to&nbsp;<a href=\"https:\/\/www.mass.gov\/how-to\/request-a-vanity-license-plate\">request a vanity license plate<\/a>&nbsp;for your car, with your choice of letters and numbers instead of random ones. Among the requirements, though, are:<br>\u5728\u54c8\u4f5b\u5927\u5b66\u6240\u5728\u7684\u9a6c\u8428\u8bf8\u585e\u5dde\uff0c\u53ef\u4ee5<a href=\"https:\/\/www.mass.gov\/how-to\/request-a-vanity-license-plate\">\u4e3a\u60a8\u7684\u6c7d\u8f66\u7d22\u53d6\u4e00\u4e2a\u865a\u8363\u8f66\u724c&nbsp;<\/a>\uff0c\u60a8\u53ef\u4ee5\u9009\u62e9\u5b57\u6bcd\u548c\u6570\u5b57\uff0c\u800c\u4e0d\u662f\u968f\u673a\u7684\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e9b\u8981\u6c42\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u201cAll vanity plates must start with at least two letters.\u201d<br>\u201c\u6240\u6709\u68b3\u5986\u53f0\u5fc5\u987b\u81f3\u5c11\u4ee5\u4e24\u4e2a\u5b57\u6bcd\u5f00\u5934\u3002\u201d<\/li>\n\n\n\n<li>\u201c\u2026 vanity plates may contain a maximum of 6 characters (letters or numbers) and a minimum of 2 characters.\u201d<br>\u201c\u2026\u68b3\u5986\u53f0\u6700\u591a\u53ef\u5305\u542b 6 \u4e2a\u5b57\u7b26\uff08\u5b57\u6bcd\u6216\u6570\u5b57\uff09\uff0c\u6700\u5c11\u53ef\u5305\u542b 2 \u4e2a\u5b57\u7b26\u3002<\/li>\n\n\n\n<li>\u201cNumbers cannot be used in the middle of a plate; they must come at the end. For example, AAA222 would be an acceptable \u2026 vanity plate; AAA22A would not be acceptable. The first number used cannot be a \u20180\u2019.\u201d<br>\u201c\u6570\u5b57\u4e0d\u80fd\u5728\u76d8\u5b50\u4e2d\u95f4\u4f7f\u7528;\u5b83\u4eec\u5fc5\u987b\u5728\u6700\u540e\u51fa\u73b0\u3002\u4f8b\u5982\uff0cAAA222 \u5c06\u662f\u4e00\u4e2a\u53ef\u4ee5\u63a5\u53d7\u7684&#8230;&#8230;\u68b3\u5986\u53f0;AAA22A \u662f\u4e0d\u53ef\u63a5\u53d7\u7684\u3002\u4f7f\u7528\u7684\u7b2c\u4e00\u4e2a\u6570\u5b57\u4e0d\u80fd\u662f&#8217;0&#8217;\u3002<\/li>\n\n\n\n<li>\u201cNo periods, spaces, or punctuation marks are allowed.\u201d<br>\u201c\u4e0d\u5141\u8bb8\u4f7f\u7528\u53e5\u70b9\u3001\u7a7a\u683c\u6216\u6807\u70b9\u7b26\u53f7\u3002\u201d<\/li>\n<\/ul>\n\n\n\n<p>In&nbsp;<code>plates.py<\/code>, implement a program that prompts the user for a vanity plate and then output&nbsp;<code>Valid<\/code>&nbsp;if meets all of the requirements or&nbsp;<code>Invalid<\/code>&nbsp;if it does not. Assume that any letters in the user\u2019s input will be uppercase. Structure your program per the below, wherein&nbsp;<code>is_valid<\/code>&nbsp;returns&nbsp;<code>True<\/code>&nbsp;if&nbsp;<code>s<\/code>&nbsp;meets all requirements and&nbsp;<code>False<\/code>&nbsp;if it does not. Assume that&nbsp;<code>s<\/code>&nbsp;will be a&nbsp;<code>str<\/code>. You\u2019re welcome to implement additional functions for&nbsp;<code>is_valid<\/code>&nbsp;to call (e.g., one function per requirement).<br>\u5728&nbsp;<code>plates.py<\/code>&nbsp;\u4e2d\uff0c\u5b9e\u73b0\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u63d0\u793a\u7528\u6237\u8f93\u5165\u68b3\u5986\u53f0\uff0c\u7136\u540e\u8f93\u51fa&nbsp;<code>Valid<\/code>&nbsp;if \u6ee1\u8db3\u6240\u6709\u8981\u6c42\uff0c\u5982\u679c\u4e0d\u6ee1\u8db3\uff0c\u5219\u8f93\u51fa&nbsp;<code>Invalid<\/code>\u3002\u5047\u8bbe\u7528\u6237\u8f93\u5165\u4e2d\u7684\u4efb\u4f55\u5b57\u6bcd\u90fd\u662f\u5927\u5199\u7684\u3002\u6309\u7167\u4ee5\u4e0b\u65b9\u5f0f\u6784\u5efa\u60a8\u7684\u7a0b\u5e8f\uff0c\u5176\u4e2d\u5982\u679c s \u6ee1\u8db3\u6240\u6709\u8981\u6c42\uff0c<code><\/code>&nbsp;\u5219&nbsp;<code>is_valid \u8fd4\u56de True<\/code>\uff0c\u5982\u679c<code>\u4e0d<\/code>\u6ee1\u8db3\uff0c\u5219\u8fd4\u56de&nbsp;<code>False<\/code>\u3002\u5047\u8bbe&nbsp;<code>s<\/code>&nbsp;\u5c06\u662f\u4e00\u4e2a&nbsp;<code>str<\/code>\u3002\u6b22\u8fce\u4f60\u5b9e\u73b0\u5176\u4ed6\u51fd\u6570\u4f9b&nbsp;<code>is_valid<\/code>&nbsp;\u8c03\u7528\uff08\u4f8b\u5982\uff0c\u6bcf\u4e2a\u9700\u6c42\u4e00\u4e2a\u51fd\u6570\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>def<\/strong> main():\n    plate = input(\"Plate: \")\n    <strong>if<\/strong> is_valid(plate):\n        print(\"Valid\")\n    <strong>else<\/strong>:\n        print(\"Invalid\")\n\n\n<strong>def<\/strong> is_valid(s):\n    ...\n\n\nmain()<\/code><\/pre>\n\n\n\n<p>Submit:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def main():\n    plate = input(\"Plate: \")\n    if is_valid(plate):\n        print(\"Valid\")\n    else:\n        print(\"Invalid\")\n\ndef two_letters(s):\n    return len(s) >= 2 and s&#91;0].isupper() and s&#91;1].isupper()\n\ndef plates_len(s):\n    return 2 &lt;= len(s) &lt;= 6\n\ndef end_numbers(s):\n    number_start = False\n\n    for i in s:\n        if i.isdigit():\n            if not number_start:\n                number_start = True\n                if i == '0':\n                    return False\n        else:\n            if number_start:\n                return False\n    return True\n\ndef no_periods_spaces_punctuation(s):\n    for i in s:\n        if not (i.isdigit() or i.isupper()):\n            return False\n    return True\n\ndef is_valid(s):\n    if two_letters(s) and plates_len(s) and end_numbers(s) and no_periods_spaces_punctuation(s):\n        return True\n    else:\n        return False\n\nmain()\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Nutrition Facts<\/h3>\n\n\n\n<p>problem:<\/p>\n\n\n\n<p>The U.S. Food &amp; Drug Adminstration (FDA) offers&nbsp;<a href=\"https:\/\/www.fda.gov\/food\/food-labeling-nutrition\/nutrition-information-raw-fruits-vegetables-and-fish\">downloadable\/printable posters<\/a>&nbsp;that \u201cshow nutrition information for the 20 most frequently consumed raw fruits \u2026 in the United States. Retail stores are welcome to download the posters, print, display and\/or distribute them to consumers in close proximity to the relevant foods in the stores.\u201d<br>\u7f8e\u56fd\u98df\u54c1\u548c\u836f\u7269\u7ba1\u7406\u5c40 \uff08FDA\uff09 \u63d0\u4f9b<a href=\"https:\/\/www.fda.gov\/food\/food-labeling-nutrition\/nutrition-information-raw-fruits-vegetables-and-fish\">\u53ef\u4e0b\u8f7d\/\u53ef\u6253\u5370\u7684\u6d77\u62a5&nbsp;<\/a>\uff0c\u201c\u663e\u793a 20 \u79cd\u6700\u5e38\u98df\u7528\u7684\u751f\u6c34\u679c\u7684\u8425\u517b\u4fe1\u606f&#8230;&#8230;\u5728\u7f8e\u56fd\u3002\u6b22\u8fce\u96f6\u552e\u5e97\u6237\u4e0b\u8f7d\u6d77\u62a5\uff0c\u5370\u5236\u3001\u5c55\u793a\u53ca\/\u6216\u6d3e\u53d1\u7ed9\u5e97\u5185\u76f8\u5173\u98df\u54c1\u9644\u8fd1\u7684\u6d88\u8d39\u8005\u3002\u300d<\/p>\n\n\n\n<p>In a file called&nbsp;<code>nutrition.py<\/code>, implement a program that prompts&nbsp;consumers&nbsp;users to input a fruit (case-insensitively) and then outputs the number of calories in one portion of that fruit, per the&nbsp;<a href=\"https:\/\/cs50.harvard.edu\/python\/psets\/2\/nutrition\/Nutrition-Information-for-Raw-Fruits---small-PDF-Poster.pdf\">FDA\u2019s poster for fruits<\/a>, which is also&nbsp;<a href=\"https:\/\/www.fda.gov\/food\/food-labeling-nutrition\/raw-fruits-poster-text-version-accessible-version\">available as text<\/a>. Capitalization aside, assume that users will input fruits exactly as written in the poster (e.g.,&nbsp;<code>strawberries<\/code>, not&nbsp;<code>strawberry<\/code>). Ignore any input that isn\u2019t a fruit.<br>\u5728\u4e00\u4e2a\u540d\u4e3a&nbsp;<code>nutrition.py<\/code>&nbsp;\u7684\u6587\u4ef6\u4e2d\uff0c\u5b9e\u65bd\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u63d0\u793a\u6d88\u8d39\u8005\u7528\u6237\u8f93\u5165\u6c34\u679c\uff08\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff09\uff0c\u7136\u540e\u6839\u636e&nbsp;<a href=\"https:\/\/cs50.harvard.edu\/python\/psets\/2\/nutrition\/Nutrition-Information-for-Raw-Fruits---small-PDF-Poster.pdf\">FDA \u7684\u6c34\u679c\u6d77\u62a5<\/a>\u8f93\u51fa\u8be5\u6c34\u679c\u7684\u4e00\u90e8\u5206\u7684\u5361\u8def\u91cc\u6570\u91cf\uff0c\u8be5\u6d77\u62a5\u4e5f\u4ee5<a href=\"https:\/\/www.fda.gov\/food\/food-labeling-nutrition\/raw-fruits-poster-text-version-accessible-version\">\u6587\u672c\u5f62\u5f0f\u63d0\u4f9b&nbsp;<\/a>\u3002\u6487\u5f00\u5927\u5199\u4e0d\u8c08\uff0c\u5047\u8bbe\u7528\u6237\u4f1a\u5b8c\u5168\u6309\u7167\u6d77\u62a5\u4e0a\u6240\u5199\u7684\u65b9\u5f0f\u8f93\u5165\u6c34\u679c\uff08\u4f8b\u5982\uff0c<code>&nbsp;\u8349\u8393&nbsp;<\/code>\uff0c\u800c\u4e0d\u662f<code>\u8349\u8393&nbsp;<\/code>\uff09\u3002\u5ffd\u7565\u4efb\u4f55\u4e0d\u662f\u6c34\u679c\u7684\u8f93\u5165\u3002<\/p>\n\n\n\n<p>Submit:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits = {\n    \"apple\": 130,\n    \"avocado\": 50,\n    \"banana\": 110,\n    \"cantaloupe\": 50,\n    \"grapefruit\": 60,\n    \"grapes\": 90,\n    \"honeydew melon\": 50,\n    \"kiwifruit\": 90,\n    \"lemon\": 15,\n    \"lime\": 20,\n    \"Nectarine\": 60,\n    \"orange\": 80,\n    \"peach\": 60,\n    \"pear\": 100,\n    \"pineapple\": 50,\n    \"plums\": 70,\n    \"strawberries\": 50,\n    \"sweet cherries\": 100,\n    \"tangerine\": 50,\n    \"watermelon\": 80\n}\n\nitem = input(\"Item: \")\nfor k in fruits:\n    if item.lower() == k:\n        print(f\"Calories: {fruits&#91;k]}\")\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-3538","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\/3538","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=3538"}],"version-history":[{"count":5,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/posts\/3538\/revisions"}],"predecessor-version":[{"id":3543,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/posts\/3538\/revisions\/3543"}],"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=3538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/categories?post=3538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/tags?post=3538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}