{"id":1172670,"date":"2025-01-15T16:52:40","date_gmt":"2025-01-15T08:52:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1172670.html"},"modified":"2025-01-15T16:52:42","modified_gmt":"2025-01-15T08:52:42","slug":"python%e7%8c%9c%e6%95%b0%e5%ad%97%e5%a6%82%e4%bd%95%e5%8a%a0%e5%85%a5%e8%8c%83%e5%9b%b4-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1172670.html","title":{"rendered":"python\u731c\u6570\u5b57\u5982\u4f55\u52a0\u5165\u8303\u56f4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26074702\/a56fee69-f07a-4d66-a77d-fc64b1d27a8e.webp\" alt=\"python\u731c\u6570\u5b57\u5982\u4f55\u52a0\u5165\u8303\u56f4\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u52a0\u5165\u8303\u56f4\u8fdb\u884c\u731c\u6570\u5b57\u6e38\u620f\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a\u8303\u56f4\u7684\u4e0a\u4e0b\u9650\u3001\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6570\u5e76\u8ba9\u7528\u6237\u5728\u8be5\u8303\u56f4\u5185\u8fdb\u884c\u731c\u6d4b\u6765\u5b9e\u73b0\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fdd\u7528\u6237\u7684\u731c\u6d4b\u5728\u5408\u7406\u7684\u8303\u56f4\u5185\uff0c\u540c\u65f6\u4e5f\u589e\u52a0\u4e86\u6e38\u620f\u7684\u8da3\u5473\u6027\u548c\u6311\u6218\u6027\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\u8bbe\u7f6e\u8303\u56f4\u3001\u751f\u6210\u968f\u673a\u6570\u3001\u63a5\u6536\u7528\u6237\u8f93\u5165\u3001\u5224\u65ad\u8f93\u5165\u662f\u5426\u5728\u8303\u56f4\u5185\u3001\u7ed9\u4e88\u53cd\u9988\u7b49\u6b65\u9aa4\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\u5b9e\u73b0\u4e00\u4e2a\u5e26\u6709\u8303\u56f4\u7684\u731c\u6570\u5b57\u6e38\u620f\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e00\u3001\u8bbe\u7f6e\u8303\u56f4<\/strong><\/h2>\n<p><p>\u5728\u731c\u6570\u5b57\u6e38\u620f\u4e2d\u8bbe\u7f6e\u8303\u56f4\u975e\u5e38\u91cd\u8981\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u7528\u6237\u7684\u731c\u6d4b\u662f\u5728\u5408\u7406\u7684\u8303\u56f4\u5185\u8fdb\u884c\u7684\u3002\u8bbe\u7f6e\u8303\u56f4\u53ef\u4ee5\u901a\u8fc7\u53d8\u91cf\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">min_range = 1<\/p>\n<p>max_range = 100<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u731c\u6d4b\u7684\u8303\u56f4\u8bbe\u7f6e\u4e3a1\u5230100\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8c03\u6574\u8fd9\u4e2a\u8303\u56f4\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u751f\u6210\u968f\u673a\u6570<\/strong><\/h2>\n<p><p>\u751f\u6210\u4e00\u4e2a\u5728\u8bbe\u7f6e\u8303\u56f4\u5185\u7684\u968f\u673a\u6570\u662f\u6e38\u620f\u7684\u6838\u5fc3\u90e8\u5206\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>random<\/code>\u5e93\u6765\u751f\u6210\u968f\u673a\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>secret_number = random.randint(min_range, max_range)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4ee3\u7801\u4f1a\u751f\u6210\u4e00\u4e2a\u5728<code>min_range<\/code>\u548c<code>max_range<\/code>\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728<code>secret_number<\/code>\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u63a5\u6536\u7528\u6237\u8f93\u5165<\/strong><\/h2>\n<p><p>\u4e3a\u4e86\u4f7f\u7528\u6237\u80fd\u591f\u8fdb\u884c\u731c\u6d4b\uff0c\u6211\u4eec\u9700\u8981\u63a5\u6536\u7528\u6237\u7684\u8f93\u5165\u3002\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">guess = int(input(f&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u5728 {min_range} \u5230 {max_range} \u4e4b\u95f4\u7684\u6570\u5b57: &quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528<code>f-string<\/code>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u4ee5\u4fbf\u5c06\u8303\u56f4\u7684\u4fe1\u606f\u663e\u793a\u7ed9\u7528\u6237\uff0c\u5e76\u63d0\u793a\u4ed6\u4eec\u8f93\u5165\u4e00\u4e2a\u5728\u6307\u5b9a\u8303\u56f4\u5185\u7684\u6570\u5b57\u3002\u8f93\u5165\u7684\u6570\u5b57\u4f1a\u88ab\u8f6c\u6362\u4e3a\u6574\u6570\u5e76\u5b58\u50a8\u5728<code>guess<\/code>\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u5224\u65ad\u8f93\u5165\u662f\u5426\u5728\u8303\u56f4\u5185<\/strong><\/h2>\n<p><p>\u5728\u63a5\u6536\u5230\u7528\u6237\u7684\u8f93\u5165\u540e\uff0c\u9700\u8981\u5224\u65ad\u8f93\u5165\u662f\u5426\u5728\u8bbe\u5b9a\u7684\u8303\u56f4\u5185\u3002\u5982\u679c\u8f93\u5165\u4e0d\u5728\u8303\u56f4\u5185\uff0c\u9700\u8981\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while guess &lt; min_range or guess &gt; max_range:<\/p>\n<p>    guess = int(input(f&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u5728 {min_range} \u5230 {max_range} \u4e4b\u95f4\u7684\u6570\u5b57: &quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u5faa\u73af\u4f1a\u4e00\u76f4\u8fd0\u884c\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165\u7684\u6570\u5b57\u5728\u6307\u5b9a\u8303\u56f4\u5185\u4e3a\u6b62\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u7ed9\u4e88\u53cd\u9988<\/strong><\/h2>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u9700\u8981\u6839\u636e\u7528\u6237\u7684\u731c\u6d4b\u7ed9\u4e88\u53cd\u9988\u3002\u5982\u679c\u7528\u6237\u731c\u5bf9\u4e86\uff0c\u6e38\u620f\u7ed3\u675f\u5e76\u544a\u77e5\u7528\u6237\u3002\u5982\u679c\u7528\u6237\u731c\u9519\u4e86\uff0c\u63d0\u4f9b\u4e00\u4e9b\u63d0\u793a\uff0c\u6bd4\u5982\u731c\u6d4b\u7684\u6570\u5b57\u8fc7\u5927\u6216\u8fc7\u5c0f\uff0c\u5e76\u8ba9\u7528\u6237\u7ee7\u7eed\u731c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while guess != secret_number:<\/p>\n<p>    if guess &lt; secret_number:<\/p>\n<p>        guess = int(input(&quot;\u592a\u5c0f\u4e86\uff0c\u8bf7\u518d\u8bd5\u4e00\u6b21: &quot;))<\/p>\n<p>    else:<\/p>\n<p>        guess = int(input(&quot;\u592a\u5927\u4e86\uff0c\u8bf7\u518d\u8bd5\u4e00\u6b21: &quot;))<\/p>\n<p>print(&quot;\u606d\u559c\u4f60\uff0c\u731c\u5bf9\u4e86\uff01&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u5faa\u73af\u4f1a\u4e00\u76f4\u8fd0\u884c\uff0c\u76f4\u5230\u7528\u6237\u731c\u5bf9\u4e3a\u6b62\u3002\u6bcf\u6b21\u7528\u6237\u731c\u6d4b\u540e\uff0c\u90fd\u4f1a\u6839\u636e\u731c\u6d4b\u7684\u7ed3\u679c\u7ed9\u4e88\u76f8\u5e94\u7684\u53cd\u9988\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u5b8c\u6574\u4ee3\u7801<\/strong><\/h2>\n<p><p>\u5c06\u4e0a\u8ff0\u6b65\u9aa4\u6574\u5408\u5230\u4e00\u8d77\uff0c\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u5b8c\u6574\u7684\u731c\u6570\u5b57\u6e38\u620f\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>def guess_number_game(min_range, max_range):<\/p>\n<p>    secret_number = random.randint(min_range, max_range)<\/p>\n<p>    guess = int(input(f&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u5728 {min_range} \u5230 {max_range} \u4e4b\u95f4\u7684\u6570\u5b57: &quot;))<\/p>\n<p>    while guess &lt; min_range or guess &gt; max_range:<\/p>\n<p>        guess = int(input(f&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u5728 {min_range} \u5230 {max_range} \u4e4b\u95f4\u7684\u6570\u5b57: &quot;))<\/p>\n<p>    while guess != secret_number:<\/p>\n<p>        if guess &lt; secret_number:<\/p>\n<p>            guess = int(input(&quot;\u592a\u5c0f\u4e86\uff0c\u8bf7\u518d\u8bd5\u4e00\u6b21: &quot;))<\/p>\n<p>        else:<\/p>\n<p>            guess = int(input(&quot;\u592a\u5927\u4e86\uff0c\u8bf7\u518d\u8bd5\u4e00\u6b21: &quot;))<\/p>\n<p>    print(&quot;\u606d\u559c\u4f60\uff0c\u731c\u5bf9\u4e86\uff01&quot;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u8303\u56f4\u5e76\u5f00\u59cb\u6e38\u620f<\/strong><\/h2>\n<p>min_range = 1<\/p>\n<p>max_range = 100<\/p>\n<p>guess_number_game(min_range, max_range)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e2a\u4ee3\u7801\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u5e26\u6709\u8303\u56f4\u7684\u731c\u6570\u5b57\u6e38\u620f\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u4e00\u6b65\u4f18\u5316\u548c\u6269\u5c55\uff0c\u6bd4\u5982\u589e\u52a0\u731c\u6d4b\u6b21\u6570\u9650\u5236\u3001\u8bb0\u5f55\u7528\u6237\u7684\u5386\u53f2\u731c\u6d4b\u3001\u63d0\u4f9b\u66f4\u591a\u7684\u63d0\u793a\u4fe1\u606f\u7b49\u3002\u5e0c\u671b\u8fd9\u4e2a\u4f8b\u5b50\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u4f60\u73a9\u5f97\u6109\u5feb\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u731c\u6570\u5b57\u6e38\u620f\u4e2d\u8bbe\u7f6e\u6570\u5b57\u8303\u56f4\uff1f<\/strong><br \/>\u5728Python\u7684\u731c\u6570\u5b57\u6e38\u620f\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u5b9a\u4e00\u4e2a\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\u6765\u9650\u5236\u73a9\u5bb6\u53ef\u4ee5\u731c\u6d4b\u7684\u6570\u5b57\u8303\u56f4\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.randint(min_value, max_value)<\/code>\u51fd\u6570\u751f\u6210\u4e00\u4e2a\u5728\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\u3002\u7528\u6237\u8f93\u5165\u7684\u6570\u5b57\u53ef\u4ee5\u901a\u8fc7<code>input()<\/code>\u51fd\u6570\u83b7\u53d6\uff0c\u5e76\u4e0e\u751f\u6210\u7684\u968f\u673a\u6570\u8fdb\u884c\u6bd4\u8f83\u3002\u8fd9\u6837\uff0c\u6e38\u620f\u5c31\u80fd\u5728\u4f60\u8bbe\u5b9a\u7684\u8303\u56f4\u5185\u8fdb\u884c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u65e0\u6548\u6570\u5b57\uff1f<\/strong><br \/>\u4e3a\u4e86\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\uff0c\u53ef\u4ee5\u5728\u7528\u6237\u8f93\u5165\u65f6\u8fdb\u884c\u6709\u6548\u6027\u68c0\u67e5\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u7ed3\u6784\u6765\u6355\u83b7\u7528\u6237\u8f93\u5165\u7684\u5f02\u5e38\u60c5\u51b5\u3002\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u4e0d\u662f\u6570\u5b57\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fdd\u6e38\u620f\u80fd\u591f\u987a\u5229\u8fdb\u884c\u800c\u4e0d\u4f1a\u56e0\u4e3a\u65e0\u6548\u8f93\u5165\u800c\u4e2d\u65ad\u3002<\/p>\n<p><strong>\u5982\u4f55\u63d0\u4f9b\u6e38\u620f\u7684\u80dc\u5229\u6216\u5931\u8d25\u53cd\u9988\uff1f<\/strong><br \/>\u5728\u6e38\u620f\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u6761\u4ef6\u8bed\u53e5\u6765\u5224\u65ad\u7528\u6237\u7684\u731c\u6d4b\u662f\u5426\u6b63\u786e\u3002\u5982\u679c\u7528\u6237\u731c\u5bf9\u4e86\u6570\u5b57\uff0c\u53ef\u4ee5\u8f93\u51fa\u201c\u606d\u559c\u4f60\uff0c\u731c\u5bf9\u4e86\uff01\u201d\u7684\u6d88\u606f\u3002\u5982\u679c\u7528\u6237\u7684\u731c\u6d4b\u8d85\u51fa\u4e86\u8bbe\u5b9a\u7684\u8303\u56f4\uff0c\u53ef\u4ee5\u63d0\u793a\u7528\u6237\u201c\u8bf7\u5728\u8303\u56f4\u5185\u731c\u6d4b\u201d\u3002\u8fd9\u6837\u7684\u53cd\u9988\u673a\u5236\u53ef\u4ee5\u589e\u5f3a\u7528\u6237\u7684\u53c2\u4e0e\u611f\uff0c\u5e76\u4f7f\u6e38\u620f\u66f4\u52a0\u6709\u8da3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u52a0\u5165\u8303\u56f4\u8fdb\u884c\u731c\u6570\u5b57\u6e38\u620f\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a\u8303\u56f4\u7684\u4e0a\u4e0b\u9650\u3001\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6570\u5e76\u8ba9\u7528\u6237\u5728\u8be5\u8303\u56f4\u5185\u8fdb\u884c\u731c\u6d4b [&hellip;]","protected":false},"author":3,"featured_media":1172678,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172670"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1172670"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172670\/revisions"}],"predecessor-version":[{"id":1172679,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172670\/revisions\/1172679"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1172678"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1172670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1172670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1172670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}