{"id":208674,"date":"2021-01-17T08:14:34","date_gmt":"2021-01-17T00:14:34","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=208674"},"modified":"2021-01-13T14:23:17","modified_gmt":"2021-01-13T06:23:17","slug":"write-games-and-learn-python","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/write-games-and-learn-python.html","title":{"rendered":"\u7f16\u5199\u6e38\u620f\u5b66\u4e60 Python"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u901a\u8fc7\u7f16\u5199\u4e00\u4e2a\u201c\u731c\u6570\u5b57\u201d\u6e38\u620f\u6765\u63a2\u7d22 Python\uff08\u548c\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\uff09\u3002\u5728\u8fd9\u4e2a\u7cfb\u5217\u4e2d\uff0c\u6211\u4eec\u8981\u7528\u4e0d\u540c\u7684\u7f16\u7a0b\u8bed\u8a00\u7f16\u5199\u76f8\u540c\u7684\u5e94\u7528\uff0c\u4ee5\u6bd4\u8f83\u5404\u79cd\u8bed\u8a00\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u5e76\u8bf4\u660e\u4f7f\u7528\u6807\u51c6\u6d4b\u8bd5\u7a0b\u5e8f\u662f\u5b66\u4e60\u65b0\u7f16\u7a0b\u597d\u65b9\u6cd5\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u5f53\u4f60\u5b66\u4e60\u4e00\u95e8\u65b0\u7684\u7f16\u7a0b\u8bed\u8a00\u65f6\uff0c\u5173\u6ce8\u5b83\u4eec\u7684\u5171\u540c\u70b9\u662f\u4ef6\u597d\u4e8b\u3002\u53d8\u91cf\u3001\u8868\u8fbe\u5f0f\u548c\u8bed\u53e5\u662f\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u7684\u57fa\u7840\u3002\u4e00\u65e6\u4f60\u7406\u89e3\u4e86\u8fd9\u4e9b\u6982\u5ff5\uff0c\u4f60\u5c31\u53ef\u4ee5\u5f00\u59cb\u5f04\u6e05\u695a\u5176\u4f59\u7684\u4e1c\u897f\u3002<\/p>\n

\u56e0\u4e3a\u7f16\u7a0b\u8bed\u8a00\u6709\u8bb8\u591a\u76f8\u4f3c\u4e4b\u5904\uff0c\u4e00\u65e6\u4f60\u77e5\u9053\u4e00\u79cd\u8bed\u8a00\uff0c\u4f60\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u89c2\u5bdf\u5b83\u4e0e\u4f60\u6240\u77e5\u9053\u7684\u8bed\u8a00\u7684\u4e0d\u540c\u4e4b\u5904\u6765\u5b66\u4e60\u53e6\u4e00\u79cd\u8bed\u8a00\u7684\u57fa\u7840\u77e5\u8bc6\u3002\u4f7f\u7528\u4f60\u7528\u5176\u4ed6\u8bed\u8a00\u7f16\u5199\u7684\u6807\u51c6\u6d4b\u8bd5\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u8ba9\u4f60\u4e13\u6ce8\u4e8e\u8bed\u8a00\uff0c\u800c\u4e0d\u662f\u7a0b\u5e8f\u7684\u903b\u8f91\u3002<\/p>\n

\u4e3a\u4e86\u8bc1\u660e\u8fd9\u70b9\uff0c\u6211\u4eec\u6b63\u5728\u6d4b\u8bd5\u5982\u4f55\u7528\u591a\u79cd\u8bed\u8a00\u7f16\u5199\u4e00\u4e2a\u201c\u731c\u6570\u5b57\u201d\u7a0b\u5e8f\u3002\u8ba1\u7b97\u673a\u9009\u62e9\u4e00\u4e2a 1 \u5230 100 \u4e4b\u95f4\u7684\u6570\u5b57\uff0c\u7136\u540e\u8ba9\u4f60\u731c\u3002\u7a0b\u5e8f\u5faa\u73af\uff0c\u76f4\u5230\u4f60\u731c\u51fa\u6b63\u786e\u7b54\u6848\u3002<\/p>\n

\u201c\u731c\u6570\u5b57\u201d\u7a0b\u5e8f\u7ec3\u4e60\u4e86\u7f16\u7a0b\u8bed\u8a00\u7684\u51e0\u4e2a\u6982\u5ff5\uff1a<\/p>\n

    \u53d8\u91cf<\/ol>\n
      \u8f93\u5165<\/ol>\n
        \u8f93\u51fa<\/ol>\n
          \u6761\u4ef6\u5224\u65ad<\/ol>\n
            \u5faa\u73af<\/ol>\n

            \u8fd9\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u5b66\u4e60\u65b0\u7f16\u7a0b\u8bed\u8a00\u7684\u5b9e\u8df5\u5b9e\u9a8c\u3002<\/p>\n

            \u7528Python\u731c\u6570\u5b57<\/strong><\/div>\n

            \u7528 Python \u8f6f\u4ef6\u57fa\u91d1\u4f1a\u7684\u8bdd\u6765\u8bf4\u3002\u201cPython \u662f\u4e00\u79cd\u89e3\u91ca\u6027\u7684\u3001\u4ea4\u4e92\u5f0f\u7684\u3001\u9762\u5411\u5bf9\u8c61\u7684\u7a0b\u5e8f\u8bbe\u8ba1\u8bed\u8a00\uff0c\u5b83\u5305\u542b\u4e86\u6a21\u5757\u3001\u5f02\u5e38\u3001\u52a8\u6001\u7c7b\u578b\u3001\u975e\u5e38\u9ad8\u5c42\u7684\u52a8\u6001\u6570\u636e\u7c7b\u578b\u548c\u7c7b\u3002\u201d\u5b83\u662f\u4e00\u79cd\u5f88\u597d\u7684\u901a\u7528\u7f16\u7a0b\u8bed\u8a00\uff0c\u4ece\u7b80\u5355\u7684\u811a\u672c\u5230\u590d\u6742\u7684 GUI \u5e94\u7528\u90fd\u5f88\u9002\u7528\u3002<\/p>\n

            \u4f60\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u4e00\u4e2a\u7248\u672c\u7684\u201c\u731c\u6570\u5b57\u201d\u6e38\u620f\u6765\u63a2\u7d22 Python\u3002\u8fd9\u662f\u6211\u7684\u5b9e\u73b0\uff1a<\/p>\n

            import random as randomlib\r\nrandom = randomlib.randint(1, 100)\r\nprint(\"Guess a number between 1 and 100\")\r\nwhile True:\r\n    guess = int(input())\r\n    if guess < random:\r\n        print(\"Too low\")\r\n    elif guess > random:\r\n        print(\"Too high\")\r\n    else:\r\n        print(\"That's right!\")\r\n        break\r\n<\/pre>\n

            \u8981\u7ed9\u4e00\u4e2a\u53d8\u91cf\u8d4b\u503c\uff0c\u8bf7\u5217\u51fa\u53d8\u91cf\u7684\u540d\u79f0\uff0c\u7136\u540e\u662f = \u53f7\u3002\u4f8b\u5982\uff0c\u8bed\u53e5 random = 0 \u7ed9 random \u53d8\u91cf\u5206\u914d\u4e86\u4e00\u4e2a\u96f6\u503c\u3002<\/p>\n

            \u811a\u672c\u7684\u7b2c\u4e00\u884c\u5c31\u5bfc\u5165\u4e86 random \u6a21\u5757\u3002\u7531\u4e8e\u672c\u7cfb\u5217\u4e2d\u7684\u6240\u6709\u7a0b\u5e8f\u90fd\u4f7f\u7528 random \u4f5c\u4e3a\u53d8\u91cf\u7684\u540d\u79f0\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 import random as randomlib \u4ee5\u522b\u540d\u5bfc\u5165\u5b83\uff0c\u4ee5\u907f\u514d\u547d\u540d\u51b2\u7a81\u3002<\/p>\n

            \u5f88\u5c11\u6709\u51fd\u6570\u88ab\u5185\u7f6e\u5230 Python \u4e2d\uff0c\u5927\u591a\u6570\u51fd\u6570\u5fc5\u987b\u4ece\u6807\u51c6\u5e93\u4e2d\u663e\u5f0f\u5bfc\u5165\u3002random \u6807\u51c6\u5e93\u6a21\u5757\u6709\u751f\u6210\u5404\u79cd\u968f\u673a\u503c\u7684\u529f\u80fd\u3002<\/p>\n

            \u811a\u672c\u7684\u7b2c\u4e8c\u884c\u8bfb\u53d6\u51fd\u6570 randint() \u7684\u7ed3\u679c\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u540d\u4e3a random \u7684\u53d8\u91cf\u3002\u51fd\u6570\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff1a\u4e00\u4e2a\u6700\u5c0f\u503c\u548c\u4e00\u4e2a\u6700\u5927\u503c\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u8303\u56f4\u662f 1 \u5230 100\uff0c\u4ee5\u4f7f\u6e38\u620f\u5177\u6709\u8db3\u591f\u7684\u6311\u6218\u6027\u3002<\/p>\n

            \u4f60\u53ef\u4ee5\u4f7f\u7528 input() \u51fd\u6570\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u503c\u3002\u5982\u679c\u4f60\u5199 guess = int(input())\uff0cPython \u4f1a\u7b49\u5f85\u7528\u6237\u8f93\u5165\u4e00\u4e9b\u6587\u672c\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6574\u6570\uff0c\u7136\u540e\u5c06\u503c\u5b58\u50a8\u5728 guess \u53d8\u91cf\u4e2d\u3002<\/p>\n

            Python \u652f\u6301\u6761\u4ef6\u8868\u8fbe\u5f0f\u548c\u5faa\u73af\u7b49\u6d41\u7a0b\u63a7\u5236\u3002\u5728\u201c\u731c\u6570\u5b57\u201d\u6e38\u620f\u4e2d\uff0c\u53ea\u8981 guess \u4e2d\u7684\u503c\u4e0d\u7b49\u4e8e random\uff0cPython \u5c31\u4f1a\u7ee7\u7eed\u5faa\u73af\u3002<\/p>\n

            \u5982\u679c\u731c\u6d4b\u503c\u5c0f\u4e8e\u968f\u673a\u6570\uff0cPython \u4f1a\u6253\u5370 Too low\uff0c\u5982\u679c\u731c\u6d4b\u503c\u5927\u4e8e\u8fd9\u4e2a\u6570\u5b57\uff0cPython \u4f1a\u6253\u5370 Too high\u3002<\/p>\n

            \u793a\u4f8b\u8f93\u51fa<\/strong><\/div>\n

            \u73b0\u5728\u4f60\u5df2\u7ecf\u5199\u597d\u4e86 Python \u7a0b\u5e8f\uff0c\u8fd0\u884c\u5b83\u6765\u73a9\u201c\u731c\u6570\u5b57\u201d\u6e38\u620f\u3002\u6bcf\u6b21\u8fd0\u884c\u7a0b\u5e8f\uff0cPython \u90fd\u4f1a\u968f\u673a\u9009\u53d6\u4e00\u4e2a\u4e0d\u540c\u7684\u6570\u5b57\u3002\u4e3a\u4e86\u5b8c\u6210\u8fd9\u4e2a\u6e38\u620f\uff0c\u4f60\u9700\u8981\u731c\u6d4b\uff0c\u76f4\u5230\u627e\u5230\u6b63\u786e\u7684\u6570\u5b57\uff1a<\/p>\n

            $ python guess.py\r\nGuess a number between 1 and 100\r\n 50\r\nToo high\r\n 25\r\nToo high\r\n 12\r\nToo high\r\n 7\r\nToo high\r\n 3\r\nToo low\r\n 5\r\nToo low\r\n 6\r\nThat's right!\r\n<\/pre>\n

            \u5728\u5b66\u4e60\u4e00\u95e8\u65b0\u7684\u7f16\u7a0b\u8bed\u8a00\u65f6\uff0c\u8fd9\u4e2a\u201c\u731c\u6570\u5b57\u201d\u6e38\u620f\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u5165\u95e8\u7a0b\u5e8f\uff0c\u56e0\u4e3a\u5b83\u4ee5\u4e00\u79cd\u76f8\u5f53\u76f4\u63a5\u7684\u65b9\u5f0f\u7ec3\u4e60\u4e86\u51e0\u4e2a\u5e38\u89c1\u7684\u7f16\u7a0b\u6982\u5ff5\u3002\u901a\u8fc7\u5728\u4e0d\u540c\u7684\u7f16\u7a0b\u8bed\u8a00\u4e2d\u5b9e\u73b0\u8fd9\u4e2a\u7b80\u5355\u7684\u6e38\u620f\uff0c\u4f60\u53ef\u4ee5\u5c55\u793a\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u7684\u4e00\u4e9b\u6838\u5fc3\u6982\u5ff5\uff0c\u5e76\u6bd4\u8f83\u6bcf\u79cd\u8bed\u8a00\u7684\u7ec6\u8282\u3002<\/p>\n

            \u4f60\u6709\u559c\u6b22\u7684\u7f16\u7a0b\u8bed\u8a00\u5417\uff1f\u4f60\u4f1a\u5982\u4f55\u7f16\u5199\u201c\u731c\u6570\u5b57\u201d\u6e38\u620f\uff1f\u8bf7\u5173\u6ce8\u672c\u7cfb\u5217\u6587\u7ae0\uff0c\u770b\u770b\u4f60\u53ef\u80fd\u611f\u5174\u8da3\u7684\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u7684\u4f8b\u5b50\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"

            \u5f53\u4f60\u5b66\u4e60\u4e00\u95e8\u65b0\u7684\u7f16\u7a0b\u8bed\u8a00\u65f6\uff0c\u5173\u6ce8\u5b83\u4eec\u7684\u5171\u540c\u70b9\u662f\u4ef6\u597d\u4e8b\u3002\u53d8\u91cf\u3001\u8868\u8fbe\u5f0f\u548c\u8bed\u53e5\u662f\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u7684\u57fa\u7840\u3002\u4e00\u65e6\u4f60\u7406\u89e3\u4e86\u8fd9 […]<\/p>\n","protected":false},"author":1329,"featured_media":208679,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-208674","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/208674","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1329"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=208674"}],"version-history":[{"count":6,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/208674\/revisions"}],"predecessor-version":[{"id":209040,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/208674\/revisions\/209040"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/208679"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=208674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=208674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=208674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}