{"id":271,"date":"2025-10-10T11:15:00","date_gmt":"2025-10-10T03:15:00","guid":{"rendered":"http:\/\/www.pyteam.cn\/?p=271"},"modified":"2025-10-10T11:15:00","modified_gmt":"2025-10-10T03:15:00","slug":"%e6%a1%88%e4%be%8b%ef%bc%9a%e7%8c%9c%e6%95%b0%e5%ad%97%e6%b8%b8%e6%88%8f-8-5","status":"publish","type":"post","link":"http:\/\/www.pyteam.cn\/index.php\/2025\/10\/10\/%e6%a1%88%e4%be%8b%ef%bc%9a%e7%8c%9c%e6%95%b0%e5%ad%97%e6%b8%b8%e6%88%8f-8-5\/","title":{"rendered":"\u6848\u4f8b\uff1a\u731c\u6570\u5b57\u6e38\u620f 8.5"},"content":{"rendered":"\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:clamp(18px, 1.125rem, 27px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#90A4AE;--cbp-line-number-width:calc(2 * 0.6 * 1.125rem);line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#ededed;color:#8198a3\">Python<\/span><span role=\"button\" tabindex=\"0\" style=\"color:#90A4AE;display:none\" aria-label=\"\u590d\u5236\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>import random  # \u5bfc\u5165\u968f\u673a\u6570\u6a21\u5757\n\nsecret = random.randint(1, 100)  # \u751f\u62101-100\u7684\u968f\u673a\u6574\u6570\nguesses_left = 5  # \u5269\u4f59\u731c\u6d4b\u6b21\u6570\n\nwhile guesses_left > 0:\n    guess = int(input(f\"\u8bf7\u731c\u4e00\u4e2a1-100\u7684\u6570\u5b57\uff08\u8fd8\u5269{guesses_left}\u6b21\u673a\u4f1a\uff09: \"))\n    \n    if guess == secret:\n        print(\"\u606d\u559c\u4f60\uff0c\u731c\u5bf9\u4e86\uff01\")\n        break  # \u731c\u5bf9\u4e86\uff0c\u63d0\u524d\u7ed3\u675f\u5faa\u73af\n    elif guess > secret:\n        print(\"\u731c\u5927\u4e86\uff01\")\n    else:\n        print(\"\u731c\u5c0f\u4e86\uff01\")\n        \n    guesses_left = guesses_left - 1  # \u6d88\u8017\u4e00\u6b21\u673a\u4f1a\nelse:  # while\u5faa\u73af\u7684else\u5b50\u53e5\uff1a\u5f53\u5faa\u73af\u6b63\u5e38\u7ed3\u675f\uff08\u975ebreak\u9000\u51fa\uff09\u65f6\u6267\u884c\n    print(f\"\u673a\u4f1a\u7528\u5b8c\u4e86\uff0c\u7b54\u6848\u662f{secret}\u3002\u6e38\u620f\u7ed3\u675f\u3002\")<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki material-theme-lighter\" style=\"background-color: #FAFAFA\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #39ADB5; font-style: italic\">import<\/span><span style=\"color: #90A4AE\"> random  <\/span><span style=\"color: #90A4AE; font-style: italic\"># \u5bfc\u5165\u968f\u673a\u6570\u6a21\u5757<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #90A4AE\">secret <\/span><span style=\"color: #39ADB5\">=<\/span><span style=\"color: #90A4AE\"> random<\/span><span style=\"color: #39ADB5\">.<\/span><span style=\"color: #6182B8\">randint<\/span><span style=\"color: #39ADB5\">(<\/span><span style=\"color: #F76D47\">1<\/span><span style=\"color: #39ADB5\">,<\/span><span style=\"color: #6182B8\"> <\/span><span style=\"color: #F76D47\">100<\/span><span style=\"color: #39ADB5\">)<\/span><span style=\"color: #90A4AE\">  <\/span><span style=\"color: #90A4AE; font-style: italic\"># \u751f\u62101-100\u7684\u968f\u673a\u6574\u6570<\/span><\/span>\n<span class=\"line\"><span style=\"color: #90A4AE\">guesses_left <\/span><span style=\"color: #39ADB5\">=<\/span><span style=\"color: #90A4AE\"> <\/span><span style=\"color: #F76D47\">5<\/span><span style=\"color: #90A4AE\">  <\/span><span style=\"color: #90A4AE; font-style: italic\"># \u5269\u4f59\u731c\u6d4b\u6b21\u6570<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #39ADB5; font-style: italic\">while<\/span><span style=\"color: #90A4AE\"> guesses_left <\/span><span style=\"color: #39ADB5\">&gt;<\/span><span style=\"color: #90A4AE\"> <\/span><span style=\"color: #F76D47\">0<\/span><span style=\"color: #39ADB5\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #90A4AE\">    guess <\/span><span style=\"color: #39ADB5\">=<\/span><span style=\"color: #90A4AE\"> <\/span><span style=\"color: #E2931D\">int<\/span><span style=\"color: #39ADB5\">(<\/span><span style=\"color: #6182B8\">input<\/span><span style=\"color: #39ADB5\">(<\/span><span style=\"color: #9C3EDA\">f<\/span><span style=\"color: #91B859\">&quot;\u8bf7\u731c\u4e00\u4e2a1-100\u7684\u6570\u5b57\uff08\u8fd8\u5269<\/span><span style=\"color: #F76D47\">{<\/span><span style=\"color: #6182B8\">guesses_left<\/span><span style=\"color: #F76D47\">}<\/span><span style=\"color: #91B859\">\u6b21\u673a\u4f1a\uff09: &quot;<\/span><span style=\"color: #39ADB5\">))<\/span><\/span>\n<span class=\"line\"><span style=\"color: #90A4AE\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #90A4AE\">    <\/span><span style=\"color: #39ADB5; font-style: italic\">if<\/span><span style=\"color: #90A4AE\"> guess <\/span><span style=\"color: #39ADB5\">==<\/span><span style=\"color: #90A4AE\"> secret<\/span><span style=\"color: #39ADB5\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #90A4AE\">        <\/span><span style=\"color: #6182B8\">print<\/span><span style=\"color: #39ADB5\">(<\/span><span style=\"color: #39ADB5\">&quot;<\/span><span style=\"color: #91B859\">\u606d\u559c\u4f60\uff0c\u731c\u5bf9\u4e86\uff01<\/span><span style=\"color: #39ADB5\">&quot;<\/span><span style=\"color: #39ADB5\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #90A4AE\">        <\/span><span style=\"color: #39ADB5; font-style: italic\">break<\/span><span style=\"color: #90A4AE\">  <\/span><span style=\"color: #90A4AE; font-style: italic\"># \u731c\u5bf9\u4e86\uff0c\u63d0\u524d\u7ed3\u675f\u5faa\u73af<\/span><\/span>\n<span class=\"line\"><span style=\"color: #90A4AE\">    <\/span><span style=\"color: #39ADB5; font-style: italic\">elif<\/span><span style=\"color: #90A4AE\"> guess <\/span><span style=\"color: #39ADB5\">&gt;<\/span><span style=\"color: #90A4AE\"> secret<\/span><span style=\"color: #39ADB5\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #90A4AE\">        <\/span><span style=\"color: #6182B8\">print<\/span><span style=\"color: #39ADB5\">(<\/span><span style=\"color: #39ADB5\">&quot;<\/span><span style=\"color: #91B859\">\u731c\u5927\u4e86\uff01<\/span><span style=\"color: #39ADB5\">&quot;<\/span><span style=\"color: #39ADB5\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #90A4AE\">    <\/span><span style=\"color: #39ADB5; font-style: italic\">else<\/span><span style=\"color: #39ADB5\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #90A4AE\">        <\/span><span style=\"color: #6182B8\">print<\/span><span style=\"color: #39ADB5\">(<\/span><span style=\"color: #39ADB5\">&quot;<\/span><span style=\"color: #91B859\">\u731c\u5c0f\u4e86\uff01<\/span><span style=\"color: #39ADB5\">&quot;<\/span><span style=\"color: #39ADB5\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #90A4AE\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #90A4AE\">    guesses_left <\/span><span style=\"color: #39ADB5\">=<\/span><span style=\"color: #90A4AE\"> guesses_left <\/span><span style=\"color: #39ADB5\">-<\/span><span style=\"color: #90A4AE\"> <\/span><span style=\"color: #F76D47\">1<\/span><span style=\"color: #90A4AE\">  <\/span><span style=\"color: #90A4AE; font-style: italic\"># \u6d88\u8017\u4e00\u6b21\u673a\u4f1a<\/span><\/span>\n<span class=\"line\"><span style=\"color: #39ADB5; font-style: italic\">else<\/span><span style=\"color: #39ADB5\">:<\/span><span style=\"color: #90A4AE\">  <\/span><span style=\"color: #90A4AE; font-style: italic\"># while\u5faa\u73af\u7684else\u5b50\u53e5\uff1a\u5f53\u5faa\u73af\u6b63\u5e38\u7ed3\u675f\uff08\u975ebreak\u9000\u51fa\uff09\u65f6\u6267\u884c<\/span><\/span>\n<span class=\"line\"><span style=\"color: #90A4AE\">    <\/span><span style=\"color: #6182B8\">print<\/span><span style=\"color: #39ADB5\">(<\/span><span style=\"color: #9C3EDA\">f<\/span><span style=\"color: #91B859\">&quot;\u673a\u4f1a\u7528\u5b8c\u4e86\uff0c\u7b54\u6848\u662f<\/span><span style=\"color: #F76D47\">{<\/span><span style=\"color: #6182B8\">secret<\/span><span style=\"color: #F76D47\">}<\/span><span style=\"color: #91B859\">\u3002\u6e38\u620f\u7ed3\u675f\u3002&quot;<\/span><span style=\"color: #39ADB5\">)<\/span><\/span><\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":67,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,5],"tags":[],"class_list":["post-271","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-while","category-5"],"views":533,"_links":{"self":[{"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/posts\/271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/comments?post=271"}],"version-history":[{"count":1,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/posts\/271\/revisions"}],"predecessor-version":[{"id":272,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/posts\/271\/revisions\/272"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/media\/67"}],"wp:attachment":[{"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/media?parent=271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/categories?post=271"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/tags?post=271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}