{"id":3604,"date":"2025-05-29T14:50:12","date_gmt":"2025-06-04T07:34:11","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3604"},"modified":"2025-06-04T14:50:12","modified_gmt":"2025-06-04T07:34:11","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%adlookuperror%e7%9a%84%e5%8a%9f%e8%83%bd%e8%88%87%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%adlookuperror%e7%9a%84%e5%8a%9f%e8%83%bd%e8%88%87%e7%94%a8%e6%b3%95\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 LookupError\uff1a\u529f\u80fd\u3001\u7528\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff082025 \u66f4\u65b0\uff09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, LookupError, \u7a0b\u5f0f\u8a2d\u8a08, \u932f\u8aa4\u8655\u7406\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 LookupError<\/h1>\n<p>\u5728 Python \u7a0b\u5f0f\u4e2d\uff0c`LookupError` \u662f\u4e00\u7a2e\u91cd\u8981\u7684\u4f8b\u5916\uff0c\u7576\u7a0b\u5f0f\u5617\u8a66\u67e5\u627e\u4e00\u500b\u4e0d\u5b58\u5728\u7684\u540d\u7a31\uff08\u4f8b\u5982\u8b8a\u6578\u3001\u7d22\u5f15\u7b49\uff09\u6642\uff0c\u5c31\u6703\u62cb\u51fa\u6b64\u4f8b\u5916\u3002\u4e86\u89e3\u5982\u4f55\u8655\u7406 `LookupError` \u5c0d\u65bc\u64b0\u5beb\u5065\u58ef\u7684 Python \u7a0b\u5f0f\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<p>## \u4ec0\u4e48\u662f LookupError\uff1f<\/p>\n<p>`LookupError` \u662f Python \u4e2d\u6240\u6709\u67e5\u627e\u932f\u8aa4\u7684\u57fa\u790e\u7570\u5e38\u985e\u5225\u3002\u5b83\u7684\u5b50\u985e\u5225\u5305\u62ec `NameError` \u548c `IndexError`\u3002\u9019\u610f\u5473\u8457\u7576\u4f60\u5617\u8a66\u8a2a\u554f\u672a\u5b9a\u7fa9\u7684\u8b8a\u6578\u6216\u5217\u8868\u4e2d\u4e0d\u5b58\u5728\u7684\u7d22\u5f15\u6642\uff0c\u6703\u5f15\u767c `LookupError`\u3002<\/p>\n<p>### LookupError \u7684\u5e95\u5c64\u908f\u8f2f<\/p>\n<p>\u7576\u7a0b\u5f0f\u78bc\u4e2d\u51fa\u73fe\u67e5\u627e\u932f\u8aa4\u6642\uff0c\u901a\u5e38\u662f\u56e0\u70ba\u4ee5\u4e0b\u539f\u56e0\uff1a<\/p>\n<p>&#8211; \u5617\u8a66\u8a2a\u554f\u4e0d\u5b58\u5728\u7684\u8b8a\u6578\u3002<br \/>\n&#8211; \u5617\u8a66\u8a2a\u554f\u4e0d\u5728\u7bc4\u570d\u5167\u7684\u5217\u8868\u7d22\u5f15\u3002<\/p>\n<p>### \u5be6\u4f5c\u7bc4\u4f8b<\/p>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba `my_list` \u7684\u5217\u8868\uff0c\u5b83\u5305\u542b\u4e09\u500b\u5143\u7d20\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmy_list = [1, 2, 3]\n&#8220;`<\/p>\n<p>\u5982\u679c\u6211\u5011\u5617\u8a66\u8a2a\u554f `my_list` \u7684\u7b2c\u56db\u500b\u5143\u7d20\uff0c\u5247\u6703\u5f15\u767c `IndexError`\uff0c\u9019\u662f `LookupError` \u7684\u4e00\u500b\u5b50\u985e\uff1a<\/p>\n<p>&#8220;`python<br \/>\nprint(my_list[3])<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6703\u5c0e\u81f4\u4ee5\u4e0b\u932f\u8aa4\uff1a<\/p>\n<p>&#8220;`<br \/>\nTraceback (most recent call last):<br \/>\n  File &#8220;test.py&#8221;, line 2, in <module><br \/>\n    print(my_list[3])<br \/>\nIndexError: list index out of range<br \/>\n&#8220;`<\/p>\n<p>### \u6355\u7372\u8207\u8655\u7406 LookupError<\/p>\n<p>\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 `try-except` \u8a9e\u53e5\u4f86\u6355\u7372\u9019\u500b\u4f8b\u5916\uff0c\u4ee5\u907f\u514d\u7a0b\u5f0f\u5d29\u6f70\uff0c\u4e26\u63d0\u4f9b\u7528\u6236\u53cb\u597d\u7684\u932f\u8aa4\u8a0a\u606f\uff1a<\/p>\n<p>&#8220;`python<br \/>\ntry:<br \/>\n    print(my_list[3])<br \/>\nexcept LookupError:<br \/>\n    print(&#8220;\u7d22\u5f15\u8d85\u51fa\u7bc4\u570d\uff01&#8221;)<br \/>\n&#8220;`<\/p>\n<p>\u904b\u884c\u4e0a\u8ff0\u4ee3\u78bc\u6703\u986f\u793a\uff1a<\/p>\n<p>&#8220;`<br \/>\n\u7d22\u5f15\u8d85\u51fa\u7bc4\u570d\uff01<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\uff0c\u6211\u5011\u5c31\u53ef\u4ee5\u5b89\u5168\u5730\u8655\u7406\u932f\u8aa4\u800c\u4e0d\u6703\u7d42\u6b62\u7a0b\u5f0f\u3002<\/p>\n<p>## LookupError \u7684\u5b50\u985e<\/p>\n<p>### 1. NameError<\/p>\n<p>`NameError` \u5728\u5c0b\u627e\u4e00\u500b\u672a\u5b9a\u7fa9\u8b8a\u6578\u6642\u88ab\u5f15\u767c\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nprint(my_var)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6703\u5c0e\u81f4\uff1a<\/p>\n<p>&#8220;`<br \/>\nTraceback (most recent call last):<br \/>\n  File &#8220;test.py&#8221;, line 2, in <module><br \/>\n    print(my_var)<br \/>\nNameError: name &#8216;my_var&#8217; is not defined<br \/>\n&#8220;`<\/p>\n<p>### 2. IndexError<\/p>\n<p>`IndexError` \u5728\u4f60\u5617\u8a66\u8a2a\u554f\u4e0d\u5728\u5217\u8868\u4e2d\u7684\u7d22\u5f15\u6642\u88ab\u5f15\u767c\u3002\u8209\u4f8b\u4f86\u8aaa\uff1a<\/p>\n<p>&#8220;`python<br \/>\nprint(my_list[3])<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6703\u5f15\u767c\u540c\u6a23\u7684 `IndexError` \u932f\u8aa4\u3002<\/p>\n<p>## \u5982\u4f55\u6709\u6548\u8655\u7406 LookupError<\/p>\n<p>\u5728\u5be6\u969b\u958b\u767c\u4e2d\uff0c\u6355\u7372 `LookupError` \u53ef\u4ee5\u5e6b\u52a9\u6211\u5011\u5728\u767c\u751f\u932f\u8aa4\u6642\u63d0\u4f9b\u66f4\u6e05\u6670\u7684\u53cd\u994b\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u5b8c\u6574\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmy_list = [1, 2, 3]\n<p>try:<br \/>\n    index = 3<br \/>\n    print(my_list[index])<br \/>\nexcept LookupError:<br \/>\n    print(&#8220;\u7d22\u5f15\u8d85\u51fa\u7bc4\u570d\uff0c\u8acb\u6aa2\u67e5\u7d22\u5f15\u503c\uff01&#8221;)<br \/>\n&#8220;`<\/p>\n<p>### \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>\u5728\u8655\u7406\u7528\u6236\u8f38\u5165\u6216\u52d5\u614b\u751f\u6210\u7684\u6578\u64da\u6642\uff0c\u5c0d\u53ef\u80fd\u7684 `LookupError` \u9032\u884c\u9810\u6e2c\u548c\u8655\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u9019\u6a23\u80fd\u5920\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u4e26\u9632\u6b62\u7a0b\u5f0f\u5d29\u6f70\u3002<\/p>\n<p>\u5982\u679c\u4f60\u60f3\u6df1\u5165\u5b78\u7fd2 Python \u7684\u932f\u8aa4\u8655\u7406\u6a5f\u5236\uff0c\u8acb\u53c3\u8003 [\u9019\u88e1](https:\/\/vocus.cc\/article\/63d9e3e0fd89780001d9af03)\u3002<\/p>\n<p>## Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>### Q1: LookupError \u548c\u5176\u5b50\u985e\u7684\u4e3b\u8981\u5340\u5225\u662f\u4ec0\u9ebc\uff1f<\/p>\n<p>**A1:** `LookupError` \u662f\u6240\u6709\u67e5\u627e\u932f\u8aa4\u7684\u57fa\u790e\u985e\u5225\uff0c\u5176\u5b50\u985e\u5305\u62ec `NameError`\uff08\u67e5\u627e\u672a\u5b9a\u7fa9\u8b8a\u6578\uff09\u548c `IndexError`\uff08\u8a2a\u554f\u4e0d\u5b58\u5728\u7684\u5217\u8868\u7d22\u5f15\uff09\u3002<\/p>\n<p>### Q2: \u5982\u4f55\u5728 Python \u4e2d\u6355\u7372\u591a\u500b\u7570\u5e38\uff1f<\/p>\n<p>**A2:** \u53ef\u4ee5\u5728 `except` \u8a9e\u53e5\u4e2d\u4ee5\u5143\u7d44\u7684\u65b9\u5f0f\u6355\u7372\u591a\u500b\u7570\u5e38\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\ntry:<br \/>\n    # \u53ef\u80fd\u6703\u5f15\u767c\u932f\u8aa4\u7684\u4ee3\u78bc<br \/>\nexcept (NameError, IndexError) as e:<br \/>\n    print(f&#8221;\u6355\u7372\u7684\u932f\u8aa4: {e}&#8221;)<br \/>\n&#8220;`<\/p>\n<p>### Q3: \u5982\u4f55\u907f\u514d\u5f15\u767c LookupError\uff1f<\/p>\n<p>**A3:** \u5728\u8a2a\u554f\u8b8a\u6578\u6216\u5217\u8868\u7d22\u5f15\u4e4b\u524d\uff0c\u61c9\u5148\u9032\u884c\u6aa2\u67e5\uff0c\u4f8b\u5982\u4f7f\u7528 `if` \u8a9e\u53e5\u4f86\u78ba\u4fdd\u8b8a\u6578\u5df2\u5b9a\u7fa9\uff0c\u6216\u6aa2\u67e5\u7d22\u5f15\u662f\u5426\u5728\u6709\u6548\u7bc4\u570d\u5167\u3002<\/p>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0c\u7406\u89e3\u53ca\u8655\u7406 `LookupError` \u662f\u64b0\u5beb\u6709\u6548 Python \u7a0b\u5f0f\u7684\u95dc\u9375\uff0c\u9019\u4e0d\u50c5\u80fd\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u627e\u5230\u554f\u984c\uff0c\u9084\u80fd\u63d0\u5347\u7a0b\u5f0f\u7684\u7a69\u5b9a\u6027\u548c\u7528\u6236\u9ad4\u9a57\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>LookupError\u662fPython\u4e2d\u7684\u4e00\u7a2e\u4f8b\u5916\uff0c\u5b83\u53ef\u4ee5\u5728\u67e5\u627e\u9805\u76ee\u6642\u5f15\u767c\u932f\u8aa4\u3002\u672c\u6587\u5c07\u4ecb\u7d39LookupError\u7684\u529f\u80fd\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u8655\u7406\u67e5\u627e\u9805\u76ee\u6642\u767c\u751f\u7684\u932f\u8aa4\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-3604","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-W8","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3604","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=3604"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3604\/revisions"}],"predecessor-version":[{"id":12784,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3604\/revisions\/12784"}],"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=3604"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3604"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}