{"id":567,"date":"2024-12-26T23:17:21","date_gmt":"2024-12-26T15:17:21","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=567"},"modified":"2024-12-26T23:17:21","modified_gmt":"2024-12-26T15:17:21","slug":"%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86-python-2-%e5%92%8c-python-3-%e4%b9%8b%e9%97%b4%e7%9a%84%e7%94%a8%e6%88%b7%e8%be%93%e5%85%a5%e5%b7%ae%e5%bc%82","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2024\/12\/26\/%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86-python-2-%e5%92%8c-python-3-%e4%b9%8b%e9%97%b4%e7%9a%84%e7%94%a8%e6%88%b7%e8%be%93%e5%85%a5%e5%b7%ae%e5%bc%82\/","title":{"rendered":"\u5982\u4f55\u5904\u7406 Python 2 \u548c Python 3 \u4e4b\u95f4\u7684\u7528\u6237\u8f93\u5165\u5dee\u5f02"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 Python 2 \u548c Python 3 \u4e2d\uff0c\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u65b9\u5f0f\u5b58\u5728\u4e00\u4e9b\u91cd\u8981\u7684\u5dee\u5f02\uff0c\u7279\u522b\u662f <code>input()<\/code> \u548c <code>raw_input()<\/code> \u51fd\u6570\u3002\u7406\u89e3\u8fd9\u4e9b\u5dee\u5f02\u5bf9\u4e8e\u7f16\u5199\u517c\u5bb9\u4e24\u79cd\u7248\u672c\u7684\u4ee3\u7801\u975e\u5e38\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u4e24\u8005\u7684\u4e3b\u8981\u5dee\u5f02\u4ee5\u53ca\u5982\u4f55\u5904\u7406\u8fd9\u4e9b\u5dee\u5f02\u7684\u65b9\u6cd5\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>Python 2 \u4e2d\u7684 <code>input()<\/code> \u548c <code>raw_input()<\/code><\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>raw_input()<\/code><\/strong>\uff1a\u8fd9\u4e2a\u51fd\u6570\u603b\u662f\u8fd4\u56de\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\uff0c\u5373\u4f7f\u7528\u6237\u8f93\u5165\u7684\u662f\u6570\u5b57\uff0c\u5b83\u4e5f\u4f1a\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u8fd4\u56de\u3002<\/li>\n\n\n\n<li><strong><code>input()<\/code><\/strong>\uff1a\u8fd9\u4e2a\u51fd\u6570\u4f1a\u5c1d\u8bd5\u5c06\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u4f5c\u4e3a Python \u8868\u8fbe\u5f0f\u6267\u884c\uff0c\u5e76\u8fd4\u56de\u6267\u884c\u7ed3\u679c\u3002\u5b83\u901a\u5e38\u7528\u6765\u6267\u884c\u4e00\u4e9b\u52a8\u6001\u4ee3\u7801\uff08\u4f8b\u5982\u8f93\u5165\u4e00\u4e2a\u6570\u5b66\u8868\u8fbe\u5f0f\uff09\u3002\u7136\u800c\uff0c\u8fd9\u79cd\u884c\u4e3a\u53ef\u80fd\u5f15\u53d1\u5b89\u5168\u95ee\u9898\uff0c\u56e0\u4e3a\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u4f1a\u88ab\u5f53\u4f5c Python \u4ee3\u7801\u6267\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff08Python 2\uff09\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code># Python 2\nname = raw_input(\"Enter your name: \")\nprint(\"Hello, \" + name)\n\nage = input(\"Enter your age: \")  # \u8f93\u5165\u5982 25 \u4f1a\u8fd4\u56de\u6574\u6570 25\nprint(\"You are \" + str(age) + \" years old.\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>raw_input()<\/code> \u8fd4\u56de\u5b57\u7b26\u4e32\uff0c\u800c <code>input()<\/code> \u8bd5\u56fe\u5c06\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u5f53\u4f5c Python \u8868\u8fbe\u5f0f\u6267\u884c\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>Python 3 \u4e2d\u7684 <code>input()<\/code><\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Python 3 \u4e2d\uff0c<code>input()<\/code> \u548c Python 2 \u4e2d\u7684 <code>raw_input()<\/code> \u76f8\u4f3c\uff1a\u5b83\u603b\u662f\u5c06\u7528\u6237\u8f93\u5165\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fd4\u56de\u3002\u800c Python 2 \u4e2d\u7684 <code>input()<\/code> \u884c\u4e3a\u88ab\u5e9f\u5f03\u4e86\uff0c\u5df2\u88ab\u5220\u9664\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff08Python 3\uff09\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code># Python 3\nname = input(\"Enter your name: \")\nprint(\"Hello, \" + name)\n\nage = input(\"Enter your age: \")  # \u8f93\u5165\u5982 25 \u4f1a\u8fd4\u56de\u5b57\u7b26\u4e32 \"25\"\nprint(\"You are \" + age + \" years old.\")  # \u9700\u8981\u8f6c\u6362\u6210\u6574\u6570<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Python 3 \u4e2d\uff0c<code>input()<\/code> \u53ea\u8fd4\u56de\u5b57\u7b26\u4e32\uff0c\u56e0\u6b64\u5982\u679c\u4f60\u60f3\u5c06\u8f93\u5165\u7684\u5185\u5bb9\u5f53\u4f5c\u6574\u6570\u6216\u6d6e\u70b9\u6570\u5904\u7406\uff0c\u4f60\u9700\u8981\u663e\u5f0f\u5730\u8f6c\u6362\u5b83\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>age = int(input(\"Enter your age: \"))  # \u8f6c\u6362\u4e3a\u6574\u6570\nprint(\"You are \" + str(age) + \" years old.\")<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u5982\u4f55\u4f7f\u4ee3\u7801\u517c\u5bb9 Python 2 \u548c Python 3<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4e86\u4f7f\u4ee3\u7801\u5728 Python 2 \u548c Python 3 \u4e4b\u95f4\u517c\u5bb9\uff0c\u6700\u597d\u4f7f\u7528 <code>six<\/code> \u6216 <code>future<\/code> \u5e93\uff0c\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u8de8\u7248\u672c\u517c\u5bb9\u7684\u5de5\u5177\u548c\u529f\u80fd\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528 <code>six<\/code> \u5e93<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>six<\/code> \u662f\u4e00\u4e2a\u7528\u4e8e\u7f16\u5199\u517c\u5bb9 Python 2 \u548c 3 \u7684\u4ee3\u7801\u7684\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7edf\u4e00\u7684\u63a5\u53e3\u6765\u5904\u7406 <code>input()<\/code> \u548c <code>raw_input()<\/code> \u7684\u5dee\u5f02\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import six\n\n# \u4f7f\u7528 six.input() \u6765\u786e\u4fdd\u5728 Python 2 \u548c 3 \u4e2d\u90fd\u6709\u76f8\u540c\u7684\u884c\u4e3a\nname = six.moves.input(\"Enter your name: \")\nprint(\"Hello, \" + name)\n\nage = int(six.moves.input(\"Enter your age: \"))  # \u8f93\u5165\u4f1a\u88ab\u8f6c\u6362\u4e3a\u6574\u6570\nprint(\"You are \" + str(age) + \" years old.\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>six.moves.input<\/code> \u4f1a\u6839\u636e Python \u7248\u672c\u81ea\u52a8\u9009\u62e9\u6b63\u786e\u7684\u8f93\u5165\u65b9\u6cd5\uff08Python 2 \u4f7f\u7528 <code>raw_input()<\/code>\uff0cPython 3 \u4f7f\u7528 <code>input()<\/code>\uff09\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528 <code>future<\/code> \u5e93<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u53e6\u4e00\u4e2a\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 <code>future<\/code> \u5e93\uff0c\u7279\u522b\u662f\u5728 Python 2 \u4e2d\u901a\u8fc7 <code>from __future__ import<\/code> \u6765\u542f\u7528 Python 3 \u7684\u884c\u4e3a\u3002\u4f60\u53ef\u4ee5\u542f\u7528 Python 3 \u98ce\u683c\u7684 <code>input()<\/code>\uff0c\u4ece\u800c\u907f\u514d\u9700\u8981\u5224\u65ad Python \u7248\u672c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from __future__ import print_function  # Python 3 \u98ce\u683c\u7684 print()\nfrom __future__ import absolute_import  # \u5bfc\u5165\u529f\u80fd\nfrom __future__ import division  # \u6d6e\u70b9\u9664\u6cd5\n\n# \u5728 Python 2 \u4e2d\u542f\u7528 Python 3 \u98ce\u683c\u7684 input()\nname = input(\"Enter your name: \")  # \u5728 Python 2 \u4e2d\u4e5f\u4f1a\u8868\u73b0\u4e3a input()\nprint(\"Hello, \" + name)\n\nage = int(input(\"Enter your age: \"))  # \u8f93\u5165\u4f1a\u8f6c\u6362\u4e3a\u6574\u6570\nprint(\"You are \" + str(age) + \" years old.\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0cPython 2 \u4f1a\u6a21\u62df Python 3 \u7684\u884c\u4e3a\uff0c\u4f46\u9700\u8981\u6ce8\u610f\uff0c<code>from __future__ import<\/code> \u8bed\u53e5\u901a\u5e38\u53ea\u5728 Python 2 \u4e2d\u4f7f\u7528\uff0cPython 3 \u4e2d\u6ca1\u6709\u5fc5\u8981\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u4f7f\u7528 <code>sys.version_info<\/code> \u5224\u65ad Python \u7248\u672c<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u4e0d\u60f3\u4f7f\u7528\u989d\u5916\u7684\u5e93\uff08\u5982 <code>six<\/code> \u6216 <code>future<\/code>\uff09\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7 <code>sys.version_info<\/code> \u6765\u68c0\u67e5\u5f53\u524d\u7684 Python \u7248\u672c\uff0c\u5e76\u6839\u636e\u7248\u672c\u6765\u9009\u62e9\u6b63\u786e\u7684\u8f93\u5165\u65b9\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import sys\n\nif sys.version_info&#91;0] &lt; 3:\n    name = raw_input(\"Enter your name: \")  # Python 2\nelse:\n    name = input(\"Enter your name: \")  # Python 3\n\nprint(\"Hello, \" + name)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Python 2<\/strong> \u4e2d\uff0c\u4f7f\u7528 <code>raw_input()<\/code> \u83b7\u53d6\u5b57\u7b26\u4e32\u8f93\u5165\uff0c\u4f7f\u7528 <code>input()<\/code> \u83b7\u53d6\u7528\u6237\u8f93\u5165\u5e76\u5c06\u5176\u4f5c\u4e3a Python \u8868\u8fbe\u5f0f\u6267\u884c\u3002<\/li>\n\n\n\n<li><strong>Python 3<\/strong> \u4e2d\uff0c<code>input()<\/code> \u59cb\u7ec8\u8fd4\u56de\u5b57\u7b26\u4e32\uff0c\u4e0d\u518d\u5b58\u5728 <code>raw_input()<\/code>\u3002<\/li>\n\n\n\n<li>\u901a\u8fc7\u4f7f\u7528 <code>six<\/code> \u6216 <code>future<\/code> \u5e93\uff0c\u53ef\u4ee5\u5b9e\u73b0\u8de8\u7248\u672c\u517c\u5bb9\u7684\u7528\u6237\u8f93\u5165\u5904\u7406\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>sys.version_info<\/code> \u4e5f\u53ef\u4ee5\u624b\u52a8\u5224\u65ad Python \u7248\u672c\uff0c\u9009\u62e9\u5408\u9002\u7684\u8f93\u5165\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u9009\u62e9\u5408\u9002\u7684\u89e3\u51b3\u65b9\u6848\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7f16\u5199\u517c\u5bb9 Python 2 \u548c 3 \u7684\u4ee3\u7801\uff0c\u907f\u514d\u7248\u672c\u5dee\u5f02\u5e26\u6765\u7684\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python 2 \u548c Python 3 \u4e2d\uff0c\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u65b9\u5f0f\u5b58\u5728\u4e00\u4e9b\u91cd\u8981\u7684\u5dee\u5f02\uff0c\u7279\u522b\u662f input()  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54],"tags":[],"class_list":["post-567","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=567"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/567\/revisions"}],"predecessor-version":[{"id":568,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/567\/revisions\/568"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}