{"id":205459,"date":"2025-05-29T15:42:44","date_gmt":"2025-05-29T07:42:44","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205459\/"},"modified":"2025-05-29T15:42:44","modified_gmt":"2025-05-29T07:42:44","slug":"simple-number-guessing-game-in-python","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205459\/","title":{"rendered":"Simple Number Guessing Game in Python"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Simple Number Guessing Game in Python<\/h1>\n<p>\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aSimple Number Guessing Game in Python\u300b<\/span>\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p>\u8fd9\u4e2a python \u7a0b\u5e8f\u662f\u4e00\u4e2a\u6709\u8da3\u4e14\u5f15\u4eba\u5165\u80dc\u7684<strong>\u731c\u6570\u5b57\u6e38\u620f<\/strong>\uff0c\u7528\u6237\u6709\u4e94\u6b21\u673a\u4f1a\u731c\u6d4b 1 \u5230 100 \u4e4b\u95f4\u968f\u673a\u751f\u6210\u7684\u6570\u5b57\u3002\u4ee5\u4e0b\u662f\u6e38\u620f\u5982\u4f55\u5de5\u4f5c\u4ee5\u53ca\u4ee3\u7801\u5982\u4f55\u8fd0\u884c\u7684\u8be6\u7ec6\u8bf4\u660e:<\/p>\n<hr>\n<ol>\n<li>\n<p><strong>\u6b22\u8fce\u8baf\u606f<\/strong><\/p>\n<p> \u6e38\u620f\u5f00\u59cb\u65f6\uff0c\u7528\u6237\u4f1a\u6536\u5230\u4e00\u6761\u6709\u8da3\u7684\u6d88\u606f\uff1a<\/p>\n<blockquote>\n<p><em>\u201c\u4f60\u597d\uff01\u6211\u662f isaeus guiang\uff0c\u8fd9\u662f\u4e00\u4e2a\u731c\u6570\u5b57\u6e38\u620f\u3002\u4f60\u6709 5 \u6b21\u673a\u4f1a\u731c\u51fa\u6570\u5b57\uff0c\u5426\u5219&#8230;\u4f60\u4f1a\u6b7b:)\u3002\u201d<\/em><\/p>\n<\/blockquote>\n<\/li>\n<li>\n<p><strong>\u968f\u673a\u6570\u751f\u6210<\/strong><\/p>\n<p> \u8be5\u7a0b\u5e8f\u4f7f\u7528 random.randint(1, 100) \u751f\u6210 1 \u5230 100 \u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002\u8fd9\u4e2a\u6570\u5b57\u5c06\u662f\u7528\u6237\u5fc5\u987b\u731c\u6d4b\u7684\u76ee\u6807\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u7528\u6237\u8f93\u5165\u548c\u9a8c\u8bc1<\/strong> <\/p>\n<ul>\n<li>\u7528\u6237\u6709\u4e94\u6b21\u5c1d\u8bd5\u731c\u6d4b\u6570\u5b57\u7684\u673a\u4f1a\u3002<\/li>\n<li>\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u4e0d\u662f\u6709\u6548\u6574\u6570\uff0c\u7cfb\u7edf\u4f1a\u63d0\u793a\u4ed6\u4eec\u518d\u6b21\u8f93\u5165\u6570\u5b57\uff0c\u800c\u4e0d\u4f1a\u5931\u8d25\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u731c\u6d4b\u53cd\u9988<\/strong><\/p>\n<p> \u6bcf\u6b21\u731c\u6d4b\u540e\uff0c\u7a0b\u5e8f\u90fd\u4f1a\u63d0\u4f9b\u53cd\u9988\uff1a<\/p>\n<ul>\n<li>\u5982\u679c\u731c\u6d4b<strong>\u592a\u4f4e<\/strong>\uff0c\u5219\u6d88\u606f\u4e3a\uff1a<em>\u201c\u592a\u4f4e\uff01\u518d\u8bd5\u4e00\u6b21\u3002\u201d<\/em> <\/li>\n<li>\u5982\u679c\u731c\u6d4b<strong>\u592a\u9ad8<\/strong>\uff0c\u5219\u6d88\u606f\u4e3a\uff1a<em>\u201c\u592a\u9ad8\uff01\u518d\u8bd5\u4e00\u6b21\u3002\u201d<\/em> <\/li>\n<li>\u5982\u679c\u731c\u6d4b\u6b63\u786e\uff0c\u7a0b\u5e8f\u4f1a\u795d\u8d3a\u7528\u6237\u5e76\u9000\u51fa\u6e38\u620f\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6e38\u620f\u7ed3\u675f<\/strong> <\/p>\n<ul>\n<li>\u5982\u679c\u7528\u6237\u5c1d\u8bd5\u5b8c\u6240\u6709\u6b21\u6570\u800c\u6ca1\u6709\u731c\u51fa\u6570\u5b57\uff0c\u5219\u7a0b\u5e8f\u4f1a\u663e\u793a\u6b63\u786e\u7684\u6570\u5b57\u5e76\u663e\u793a\u4ee5\u4e0b\u6d88\u606f\uff1a &gt; *\u201c\u54ce\u5440\uff0c\u62b1\u6b49\uff0c\u53f7\u7801\u662f[\u53f7\u7801]\u3002\u4f60\u4f1a\u6b7b\u7684:)\u3002\u201d<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<hr>\n<ol>\n<li>\n<p><strong>\u968f\u673a\u6570\u751f\u6210<\/strong><\/p>\n<p> \u6e38\u620f\u4f7f\u7528 random.randint(1, 100) \u6765\u521b\u5efa\u4e0d\u53ef\u9884\u6d4b\u7684\u6570\u5b57\uff0c\u786e\u4fdd\u6bcf\u6b21\u6e38\u620f\u90fd\u6709\u516c\u5e73\u4e14\u4ee4\u4eba\u5174\u594b\u7684\u6311\u6218\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u8f93\u5165\u9a8c\u8bc1<\/strong><\/p>\n<p> \u8be5\u4ee3\u7801\u4f7f\u7528 try- except \u5757\u6765\u4f18\u96c5\u5730\u5904\u7406\u9519\u8bef\uff0c\u4ece\u800c\u9632\u6b62\u65e0\u6548\u8f93\u5165\uff08\u5982\u5b57\u6bcd\u6216\u7b26\u53f7\uff09\u8ba1\u4e3a\u5c1d\u8bd5\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5c1d\u8bd5\u8ddf\u8e2a<\/strong><\/p>\n<p> guess_counter \u53d8\u91cf\u8ddf\u8e2a\u7528\u6237\u8fdb\u884c\u4e86\u591a\u5c11\u6b21\u731c\u6d4b\uff0c\u786e\u4fdd\u7528\u6237\u6070\u597d\u83b7\u5f97\u4e94\u6b21\u673a\u4f1a\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u52a8\u6001\u53cd\u9988<\/strong><\/p>\n<p> \u6bcf\u6b21\u731c\u9519\u540e\uff0c\u6e38\u620f\u90fd\u4f1a\u63d0\u4f9b\u6709\u7528\u7684\u63d0\u793a\uff0c\u5f15\u5bfc\u73a9\u5bb6\u731c\u51fa\u6b63\u786e\u7684\u6570\u5b57\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5e7d\u9ed8\u8f6c\u6298<\/strong><\/p>\n<p> \u4fcf\u76ae\u7684\u4fe1\u606f\u4e3a\u6e38\u620f\u589e\u6dfb\u4e86\u6709\u8da3\u3001\u8f7b\u677e\u7684\u57fa\u8c03\uff0c\u4f7f\u5176\u66f4\u52a0\u6709\u8da3\u3002<\/p>\n<\/li>\n<\/ol>\n<hr>\n<ol>\n<li> <strong>\u6e38\u620f\u5f00\u59cb\uff1a<\/strong> <\/li>\n<\/ol>\n<pre>   hello! this is isaeus guiang, and this is a number guessing game. \n   you have 5 chances to guess the number, or...\n   you will die :).\n<\/pre>\n<ol>\n<li> <strong>\u7528\u6237\u505a\u51fa\u731c\u6d4b\uff1a<\/strong> <\/li>\n<\/ol>\n<pre>   attempt 1\/5 - your guess :) -&gt; 50\n   too high! try again.\n   attempt 2\/5 - your guess :) -&gt; 25\n   too low! try again.\n   attempt 3\/5 - your guess :) -&gt; 37\n   too low! try again.\n   attempt 4\/5 - your guess :) -&gt; 43\n   too high! try again.\n   attempt 5\/5 - your guess :) -&gt; 40\n   oops, sorry, the number was 42. you are going to die :).\n<\/pre>\n<ol>\n<li> <strong>\u6b63\u786e\u731c\u6d4b\uff085 \u6b21\u5c1d\u8bd5\u4e4b\u524d\uff09\uff1a<\/strong> <\/li>\n<\/ol>\n<pre>   Attempt 3\/5 - Your Guess :) -&gt; 42\n   The number is 42, and you guessed it right in 3 attempt(s)!\n<\/pre>\n<hr>\n<p>\u8fd9\u4e2a\u7a0b\u5e8f\u662f\u7ec3\u4e60\u57fa\u672c python \u6982\u5ff5\u7684\u597d\u65b9\u6cd5\uff0c\u5305\u62ec\u5faa\u73af\u3001\u6761\u4ef6\u3001\u7528\u6237\u8f93\u5165\u548c\u9519\u8bef\u5904\u7406\uff0c\u540c\u65f6\u8fd8\u53ef\u4ee5\u4eab\u53d7\u6709\u8da3\u7684\u731c\u8c1c\u6e38\u620f\uff01<\/p>\n<p>\u6b22\u8fce\u4f7f\u7528\u6211\u7684 github \u4e0a\u7684\u4ee3\u7801\uff1ahttps:\/\/github.com\/asiguiang\/simple-python-game-number-guessing?tab=readme-ov-file#number-guessing-game-overview<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241202\/1733141761674da5019806c.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u4f5c\u8005\u7b80\u4ecb\uff1a<\/strong><\/p>\n<blockquote>\n<p>\u4f0a\u8428\u4e4c\u65af\u00b7\u201c\u963f\u897f\u201dguang<br \/><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241202\/1733141761674da5019b8cf.jpg\" class=\"aligncenter\"><br \/> \u83f2\u5f8b\u5bbe aws \u4e91\u4ff1\u4e50\u90e8\u7684\u533a\u57df\u961f\u957f\u548c\u83f2\u5f8b\u5bbe\u7406\u5de5\u5927\u5b66\u7684\u4e00\u540d\u70ed\u5fc3\u5b66\u751f\u3002\u51ed\u501f\u5bf9\u4e91\u8ba1\u7b97\u548c\u7f51\u7edc\u5b89\u5168\u7684\u5f3a\u70c8\u70ed\u60c5\uff0casi \u4f5c\u4e3a aws \u4e91\u4ff1\u4e50\u90e8 &#8211; pup manila \u7684\u524d\u961f\u957f\u53d1\u6325\u4e86\u5173\u952e\u4f5c\u7528\uff0c\u63a8\u52a8\u4e86\u6280\u672f\u793e\u533a\u7684\u534f\u4f5c\u548c\u521b\u65b0\u3002<\/p>\n<p>asi \u76ee\u524d\u6b63\u5728\u653b\u8bfb\u8ba1\u7b97\u673a\u79d1\u5b66\u5b66\u58eb\u5b66\u4f4d\uff0c\u62e5\u6709\u4e30\u5bcc\u7684\u7ecf\u9a8c\uff0c\u5305\u62ec\u62c5\u4efb aws re\/start \u8bfe\u7a0b\u534f\u8c03\u5458\u5b9e\u4e60\u751f\u548c\u8f6f\u6280\u80fd\u590d\u4e60\u8bb2\u5e08\u3002\u4ed6\u6559\u6388\u5b9e\u7528\u7684 aws \u7814\u8ba8\u4f1a\uff0c\u73b0\u5728\u662f globe \u7684\u5b89\u5168\u98ce\u9669\u8bc4\u4f30\u548c\u5408\u89c4\u5b9e\u4e60\u751f\uff0c\u589e\u5f3a\u4e86\u4ed6\u5728\u8be5\u9886\u57df\u7684\u4e13\u4e1a\u77e5\u8bc6\u3002<\/p>\n<p>\u76ee\u524d\u6b63\u5728\u7814\u7a76\u5e76\u4e13\u6ce8\u4e8e\u4e91\u91d1\u878d\u8fd0\u8425\u548c\u5b89\u5168\u5408\u89c4\u3002<\/p>\n<\/blockquote>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aSimple Number Guessing Game in Python\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Simple Number Gu&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205459","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205459","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205459"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205459\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}