{"id":3016,"date":"2025-06-02T16:23:34","date_gmt":"2025-06-04T09:09:34","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3016"},"modified":"2025-06-04T16:23:34","modified_gmt":"2025-06-04T09:09:34","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8python%e7%9a%84input%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8python%e7%9a%84input%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Python \u7684 input() \u51fd\u6578\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, input(), Python \u6559\u5b78, Python \u5be6\u4f5c\u7bc4\u4f8b\"><\/p>\n<h2>Python \u4e2d\u7684 input() \u51fd\u5f0f<\/h2>\n<p>\u5728 Python \u7a0b\u5f0f\u8a2d\u8a08\u4e2d\uff0c`input()` \u51fd\u5f0f\u662f\u4e00\u500b\u975e\u5e38\u91cd\u8981\u7684\u51fd\u5f0f\uff0c\u5b83\u53ef\u4ee5\u8b93\u4f7f\u7528\u8005\u8f38\u5165\u8cc7\u6599\uff0c\u4f7f\u7a0b\u5f0f\u7684\u904b\u884c\u66f4\u52a0\u9748\u6d3b\u548c\u4e92\u52d5\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u4ecb\u7d39 `input()` \u51fd\u5f0f\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u4f5c\u7bc4\u4f8b\uff0c\u5e6b\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u5176\u529f\u80fd\u3002<\/p>\n<p>### \u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/p>\n<p>`input()` \u51fd\u5f0f\u63a5\u53d7\u4e00\u500b\u5b57\u4e32\u53c3\u6578\uff0c\u8a72\u53c3\u6578\u4f5c\u70ba\u63d0\u793a\u986f\u793a\u5728\u4f7f\u7528\u8005\u8f38\u5165\u8cc7\u6599\u4e4b\u524d\u3002\u4ee5\u4e0b\u662f\u57fa\u672c\u7684\u7528\u6cd5\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nname = input(&#8220;\u8acb\u8f38\u5165\u4f60\u7684\u540d\u5b57\uff1a&#8221;)<br \/>\nprint(&#8220;\u4f60\u597d\uff0c&#8221; + name)<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u6bb5\u7a0b\u5f0f\u78bc\u4e2d\uff0c`input()` \u51fd\u5f0f\u6703\u986f\u793a\u300c\u8acb\u8f38\u5165\u4f60\u7684\u540d\u5b57\uff1a\u300d\uff0c\u4f7f\u7528\u8005\u8f38\u5165\u7684\u540d\u5b57\u6703\u88ab\u5b58\u5132\u5230\u8b8a\u6578 `name` \u4e2d\uff0c\u63a5\u8457\u7a0b\u5f0f\u6703\u986f\u793a\u300c\u4f60\u597d\uff0cXXX\u300d\uff0c\u5176\u4e2d `XXX` \u70ba\u4f7f\u7528\u8005\u8f38\u5165\u7684\u540d\u5b57\u3002<\/p>\n<p>### \u932f\u8aa4\u8655\u7406<\/p>\n<p>\u5728\u4f7f\u7528 `input()` \u51fd\u5f0f\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u4f8b\u5982\u4f7f\u7528\u8005\u672a\u8f38\u5165\u8cc7\u6599\u3002\u70ba\u4e86\u63d0\u9ad8\u7a0b\u5f0f\u7684\u5065\u58ef\u6027\uff0c\u53ef\u4ee5\u9032\u884c\u932f\u8aa4\u8655\u7406\uff1a<\/p>\n<p>&#8220;`python<br \/>\ntry:<br \/>\n    name = input(&#8220;\u8acb\u8f38\u5165\u4f60\u7684\u540d\u5b57\uff1a&#8221;)<br \/>\n    if not name:<br \/>\n        raise ValueError(&#8220;\u540d\u5b57\u4e0d\u80fd\u70ba\u7a7a\uff01&#8221;)<br \/>\n    print(&#8220;\u4f60\u597d\uff0c&#8221; + name)<br \/>\nexcept ValueError as e:<br \/>\n    print(e)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u6703\u6aa2\u67e5\u4f7f\u7528\u8005\u662f\u5426\u8f38\u5165\u4e86\u540d\u5b57\uff0c\u82e5\u672a\u8f38\u5165\u5247\u62cb\u51fa\u932f\u8aa4\u3002<\/p>\n<p>### \u9ad8\u7d1a\u7528\u6cd5<\/p>\n<p>`input()` \u51fd\u5f0f\u672c\u8eab\u4e0d\u63a5\u53d7\u6574\u6578\u6216\u5e03\u6797\u503c\u4f5c\u70ba\u53c3\u6578\u3002\u8981\u9650\u5236\u4f7f\u7528\u8005\u8f38\u5165\u7684\u5b57\u4e32\u9577\u5ea6\u6216\u683c\u5f0f\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u984d\u5916\u7684\u908f\u8f2f\u4f86\u5be6\u73fe\u3002\u4f8b\u5982\uff0c\u9650\u5236\u4f7f\u7528\u8005\u8f38\u5165\u7684\u5bc6\u78bc\u9577\u5ea6\uff1a<\/p>\n<p>&#8220;`python<br \/>\npassword = input(&#8220;\u8acb\u8f38\u5165\u4f60\u7684\u5bc6\u78bc\uff08\u81f3\u5c118\u500b\u5b57\u5143\uff09\uff1a&#8221;)<br \/>\nwhile len(password) < 8:\n    print(\"\u5bc6\u78bc\u9577\u5ea6\u4e0d\u8db3\uff0c\u8acb\u518d\u8a66\u4e00\u6b21\u3002\")\n    password = input(\"\u8acb\u8f38\u5165\u4f60\u7684\u5bc6\u78bc\uff08\u81f3\u5c118\u500b\u5b57\u5143\uff09\uff1a\")\nprint(\"\u4f60\u7684\u5bc6\u78bc\u662f\uff1a\" + password)\n```\n\n\u5728\u9019\u6bb5\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u4f7f\u7528\u8005\u7684\u5bc6\u78bc\u5fc5\u9808\u81f3\u5c11\u70ba8\u500b\u5b57\u5143\uff0c\u5426\u5247\u6703\u63d0\u793a\u91cd\u65b0\u8f38\u5165\u3002\n\n### \u5ef6\u4f38\u61c9\u7528\n\n`input()` \u51fd\u5f0f\u7684\u61c9\u7528\u7bc4\u570d\u975e\u5e38\u5ee3\u6cdb\uff0c\u53ef\u4ee5\u7528\u65bc\u5404\u7a2e\u4e92\u52d5\u5f0f\u61c9\u7528\u7a0b\u5f0f\uff0c\u4f8b\u5982\u7c21\u6613\u7684\u8a08\u7b97\u6a5f\u3001\u554f\u7b54\u904a\u6232\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u6578\u5b57\u731c\u6e2c\u904a\u6232\u7bc4\u4f8b\uff1a\n\n```python\nimport random\n\nnumber_to_guess = random.randint(1, 100)\nattempts = 0\n\nwhile True:\n    guess = int(input(\"\u8acb\u731c\u4e00\u500b1\u5230100\u4e4b\u9593\u7684\u6578\u5b57\uff1a\"))\n    attempts += 1\n    if guess < number_to_guess:\n        print(\"\u592a\u5c0f\u4e86\uff0c\u518d\u8a66\u8a66\uff01\")\n    elif guess > number_to_guess:<br \/>\n        print(&#8220;\u592a\u5927\u4e86\uff0c\u518d\u8a66\u8a66\uff01&#8221;)<br \/>\n    else:<br \/>\n        print(f&#8221;\u606d\u559c\u4f60\uff01\u6b63\u78ba\u7b54\u6848\u662f {number_to_guess}\uff0c\u4f60\u7e3d\u5171\u731c\u4e86 {attempts} \u6b21\u3002&#8221;)<br \/>\n        break<br \/>\n&#8220;`<\/p>\n<p>\u9019\u500b\u904a\u6232\u6703\u96a8\u6a5f\u751f\u6210\u4e00\u500b1\u5230100\u4e4b\u9593\u7684\u6578\u5b57\uff0c\u4e26\u8b93\u4f7f\u7528\u8005\u4e0d\u65b7\u731c\u6e2c\uff0c\u76f4\u5230\u731c\u4e2d\u70ba\u6b62\u3002<\/p>\n<p>### \u7e3d\u7d50<\/p>\n<p>`input()` \u51fd\u5f0f\u662f Python \u4e2d\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4f86\u4f7f\u7a0b\u5f0f\u66f4\u5177\u4e92\u52d5\u6027\u3002\u900f\u904e\u9069\u7576\u7684\u932f\u8aa4\u8655\u7406\u548c\u64f4\u5c55\u61c9\u7528\uff0c\u53ef\u4ee5\u8b93\u4f60\u7684\u7a0b\u5f0f\u66f4\u52a0\u5065\u58ef\u548c\u5be6\u7528\u3002<\/p>\n<p>> \u60f3\u6df1\u5165\u5b78\u7fd2 Python \u7684\u5176\u4ed6\u529f\u80fd\u55ce\uff1f\u8acb\u53c3\u95b1 [\u9019\u7bc7\u6587\u7ae0](https:\/\/vocus.cc\/article\/61dabc4f2c5e180068f794c3) \u4f86\u7372\u53d6\u66f4\u591a\u6559\u5b78\u8cc7\u8a0a\u3002<\/p>\n<p>### Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: input() \u51fd\u5f0f\u7684\u8fd4\u56de\u503c\u662f\u4ec0\u9ebc\uff1f**<br \/>\nA1: `input()` \u51fd\u5f0f\u7684\u8fd4\u56de\u503c\u662f\u4f7f\u7528\u8005\u8f38\u5165\u7684\u5b57\u4e32\u3002\u5982\u679c\u9700\u8981\u5176\u4ed6\u985e\u578b\u7684\u6578\u64da\uff0c\u9700\u9032\u884c\u985e\u578b\u8f49\u63db\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u8655\u7406\u4f7f\u7528\u8005\u8f38\u5165\u6642\u7684\u932f\u8aa4\uff1f**<br \/>\nA2: \u53ef\u4ee5\u4f7f\u7528 `try&#8230;except` \u4f86\u6355\u6349\u932f\u8aa4\uff0c\u4e26\u6839\u64da\u9700\u8981\u63d0\u793a\u4f7f\u7528\u8005\u91cd\u65b0\u8f38\u5165\u3002<\/p>\n<p>**Q3: input() \u51fd\u5f0f\u662f\u5426\u53ef\u4ee5\u63a5\u53d7\u591a\u500b\u53c3\u6578\uff1f**<br \/>\nA3: `input()` \u51fd\u5f0f\u50c5\u63a5\u53d7\u4e00\u500b\u5b57\u4e32\u63d0\u793a\u53c3\u6578\uff0c\u82e5\u9700\u9650\u5236\u8f38\u5165\u683c\u5f0f\uff0c\u9700\u81ea\u884c\u5be6\u73fe\u908f\u8f2f\u8655\u7406\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Python\u7684Input()\u51fd\u6578\uff0c\u8b93\u4f60\u53ef\u4ee5\u8f15\u9b06\u5730\u5f9e\u7528\u6236\u7aef\u63a5\u6536\u8f38\u5165\uff0c\u4e26\u5c07\u5176\u8f49\u63db\u70baPython\u7a0b\u5f0f\u53ef\u4ee5\u8655\u7406\u7684\u683c\u5f0f\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-3016","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-ME","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3016","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3016"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3016\/revisions"}],"predecessor-version":[{"id":3017,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3016\/revisions\/3017"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}