{"id":50097,"date":"2024-12-03T17:46:49","date_gmt":"2024-12-03T09:46:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/50097\/"},"modified":"2024-12-03T17:46:49","modified_gmt":"2024-12-03T09:46:49","slug":"python-%e4%b8%ad-eval-%e5%87%bd%e6%95%b0%ef%bc%8c%e4%b8%ba%e4%bd%95%e5%9c%a8%e9%aa%8c%e8%af%81%e7%a0%81%e6%a0%a1%e9%aa%8c%e4%b8%ad%e4%bc%9a%e6%8a%9b%e5%87%ba-nameerror-%e5%bc%82%e5%b8%b8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/50097\/","title":{"rendered":"Python \u4e2d eval() \u51fd\u6570\uff0c\u4e3a\u4f55\u5728\u9a8c\u8bc1\u7801\u6821\u9a8c\u4e2d\u4f1a\u629b\u51fa NameError \u5f02\u5e38\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u4e2d eval() \u51fd\u6570\uff0c\u4e3a\u4f55\u5728\u9a8c\u8bc1\u7801\u6821\u9a8c\u4e2d\u4f1a\u629b\u51fa NameError \u5f02\u5e38\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300aPython \u4e2d eval() \u51fd\u6570\uff0c\u4e3a\u4f55\u5728\u9a8c\u8bc1\u7801\u6821\u9a8c\u4e2d\u4f1a\u629b\u51fa NameError \u5f02\u5e38\uff1f\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241111\/17313151106731c5a67218e.jpg\" class=\"aligncenter\" title=\"Python \u4e2d eval() \u51fd\u6570\uff0c\u4e3a\u4f55\u5728\u9a8c\u8bc1\u7801\u6821\u9a8c\u4e2d\u4f1a\u629b\u51fa NameError \u5f02\u5e38\uff1f\u63d2\u56fe\" alt=\"Python \u4e2d eval() \u51fd\u6570\uff0c\u4e3a\u4f55\u5728\u9a8c\u8bc1\u7801\u6821\u9a8c\u4e2d\u4f1a\u629b\u51fa NameError \u5f02\u5e38\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>python \u4e2d eval() \u51fd\u6570\uff0c\u4e00\u4e2a\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u56f0\u60d1<\/strong><\/p>\n<p>\u5728\u9a8c\u8bc1\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u83b7\u53d6\u9a8c\u8bc1\u7801\u5e76\u901a\u8fc7\u7279\u5b9a\u8868\u8fbe\u5f0f\u8fdb\u884c\u6821\u9a8c\u3002\u901a\u8fc7 eval() \u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5730\u6267\u884c\u5b57\u7b26\u4e32\u8868\u8fbe\u5f0f\uff0c\u4f46\u6709\u65f6\u4f1a\u9047\u5230\u4e00\u4e9b\u56f0\u60d1\u3002<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u60f3\u8981\u6821\u9a8c\u7528\u6237\u8f93\u5165\u7684\u9a8c\u8bc1\u7801\uff0c\u8be5\u9a8c\u8bc1\u7801\u7531\u4e24\u4e2a\u968f\u673a\u751f\u6210\u7684\u6570\u5b57\uff08\u5206\u522b\u662f a \u548c b\uff09\u76f8\u52a0\u5f97\u5230\u3002\u4f7f\u7528 eval() \u51fd\u6570\u65f6\uff0c\u5e38\u89c1\u7684\u505a\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>import random\n\na = str(random.randint(0, 10))\nb = str(random.randint(0, 10))\nx = input(f\"\u8bf7\u8f93\u5165\u8ba1\u7b97\u9a8c\u8bc1\u4fe1\u606f\u7684\u7ed3\u679c({a}+{b}):\")\nr = eval('a + b')\nif x == str(r):\n    print(\"\u60a8\u5df2\u7ecf\u901a\u8fc7\u9a8c\u8bc1\uff01\")\nelse:\n    print(\"\u60a8\u7684\u9a8c\u8bc1\u4fe1\u606f\u8f93\u5165\u6709\u8bef\uff01\")<\/pre>\n<p>\u7136\u800c\uff0c\u8fd9\u6bb5\u4ee3\u7801\u53ef\u80fd\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\uff0c\u5e76\u4e14\u4f1a\u629b\u51fa nameerror \u5f02\u5e38\u3002\u8fd9\u662f\u56e0\u4e3a eval() \u51fd\u6570\u9700\u8981\u5728\u4e0a\u4e0b\u6587\u4e2d\u67e5\u627e a \u548c b \u7684\u503c\uff0c\u800c\u5728\u8fd9\u4e2a\u4ee3\u7801\u6bb5\u4e2d\uff0ca \u548c b \u5e76\u6ca1\u5728\u4e0a\u4e0b\u6587\u4e2d\u5b9a\u4e49\u3002<\/p>\n<p>\u6b63\u786e\u7684\u505a\u6cd5\u662f\u5c06 eval() \u7684\u7b2c\u4e09\u4e2a\u53c2\u6570 locals \u6307\u5b9a\u4e3a\u4e00\u4e2a\u5305\u542b a \u548c b \u503c\u7684\u5b57\u5178\u3002\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>import random\n\na = str(random.randint(0, 10))\nb = str(random.randint(0, 10))\nx = input(f\"\u8bf7\u8f93\u5165\u8ba1\u7b97\u9a8c\u8bc1\u4fe1\u606f\u7684\u7ed3\u679c({a}+{b}):\")\nr = eval('a + b', {}, {'a': a, 'b': b})\nif x == str(r):\n    print(\"\u60a8\u5df2\u7ecf\u901a\u8fc7\u9a8c\u8bc1\uff01\")\nelse:\n    print(\"\u60a8\u7684\u9a8c\u8bc1\u4fe1\u606f\u8f93\u5165\u6709\u8bef\uff01\")<\/pre>\n<p>\u901a\u8fc7\u6307\u5b9a locals \u53c2\u6570\uff0c\u6211\u4eec\u786e\u4fdd\u4e86 eval() \u51fd\u6570\u53ef\u4ee5\u5728\u4e0a\u4e0b\u6587\u4e2d\u627e\u5230 a \u548c b \u7684\u503c\uff0c\u4ece\u800c\u51c6\u786e\u8ba1\u7b97\u51fa\u9a8c\u8bc1\u7ed3\u679c\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPython \u4e2d eval() \u51fd\u6570\uff0c\u4e3a\u4f55\u5728\u9a8c\u8bc1\u7801\u6821\u9a8c\u4e2d\u4f1a\u629b\u51fa NameError \u5f02\u5e38\uff1f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u4e2d eval() \u51fd\u6570\uff0c\u4e3a\u4f55\u5728\u9a8c\u8bc1\u7801\u6821\u9a8c\u4e2d\u4f1a\u629b\u51fa NameError \u5f02\u5e38\uff1f \u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300aPython \u4e2d eval() \u51fd\u6570\uff0c\u4e3a\u4f55\u5728\u9a8c\u8bc1\u7801\u6821\u9a8c\u4e2d\u4f1a\u629b\u51fa NameError \u5f02\u5e38\uff1f\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~ python \u4e2d eval() \u51fd\u6570\uff0c\u4e00\u4e2a\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u56f0\u60d1 \u5728\u9a8c\u8bc1\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u83b7\u53d6\u9a8c\u8bc1\u7801\u5e76\u901a\u8fc7\u7279\u5b9a\u8868\u8fbe\u5f0f\u8fdb\u884c\u6821\u9a8c\u3002\u901a\u8fc7 eval() \u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5730\u6267\u884c\u5b57\u7b26\u4e32\u8868\u8fbe\u5f0f\uff0c\u4f46\u6709\u65f6\u4f1a\u9047\u5230\u4e00\u4e9b\u56f0\u60d1\u3002 \u5047\u8bbe\u6211\u4eec\u60f3\u8981\u6821\u9a8c\u7528\u6237\u8f93\u5165\u7684\u9a8c\u8bc1\u7801\uff0c\u8be5\u9a8c\u8bc1\u7801\u7531\u4e24\u4e2a\u968f\u673a\u751f\u6210\u7684\u6570\u5b57\uff08\u5206\u522b\u662f a \u548c b\uff09\u76f8\u52a0\u5f97\u5230\u3002\u4f7f\u7528 eval() \u51fd\u6570\u65f6\uff0c\u5e38\u89c1\u7684\u505a\u6cd5\u5982\u4e0b\uff1a import random a = str(random.randint(0, 10)) b = str(random.randint(0, 10)) x = input(f&#8221;\u8bf7\u8f93\u5165\u8ba1\u7b97\u9a8c\u8bc1\u4fe1\u606f\u7684\u7ed3\u679c({a}+{b}):&#8221;) r = eval(&#8216;a + b&#8217;) if x == str(r): print(&#8220;\u60a8\u5df2\u7ecf\u901a\u8fc7\u9a8c\u8bc1\uff01&#8221;) else: print(&#8220;\u60a8\u7684\u9a8c\u8bc1\u4fe1\u606f\u8f93\u5165\u6709\u8bef\uff01&#8221;) \u7136\u800c\uff0c\u8fd9\u6bb5\u4ee3\u7801\u53ef\u80fd\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\uff0c\u5e76\u4e14\u4f1a\u629b\u51fa nameerror \u5f02\u5e38\u3002\u8fd9\u662f\u56e0\u4e3a eval() \u51fd\u6570\u9700\u8981\u5728\u4e0a\u4e0b\u6587\u4e2d\u67e5\u627e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-50097","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50097","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=50097"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50097\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=50097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=50097"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=50097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}