{"id":89547,"date":"2026-08-03T08:40:40","date_gmt":"2026-08-03T00:40:40","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/89547.html"},"modified":"2026-08-03T08:40:40","modified_gmt":"2026-08-03T00:40:40","slug":"ai%e5%a4%a7%e6%a8%a1%e5%9e%8b%e5%ad%a6%e4%b9%a0%e5%88%86%e4%ba%ab","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/89547.html","title":{"rendered":"AI\u5927\u6a21\u578b\u5b66\u4e60\u5206\u4eab"},"content":{"rendered":"<h2>Python \u96f6\u57fa\u7840 Day07 \u7b14\u8bb0&#xff5c;Lambda \u533f\u540d\u51fd\u6570\u3001\u6587\u4ef6 IO \u64cd\u4f5c\u3001\u5f02\u5e38\u6355\u83b7\u4e0e Debug \u8c03\u8bd5<\/h2>\n<h3>\u4e00\u3001\u6587\u7ae0\u524d\u8a00<\/h3>\n<p>\u672c\u7bc7\u6574\u5408 Python \u7b2c 7 \u5929\u6838\u5fc3\u77e5\u8bc6\u70b9&#xff0c;\u5305\u542b\u56db\u5927\u6838\u5fc3\u6a21\u5757&#xff1a;lambda \u533f\u540d\u51fd\u6570\u3001\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\u3001with \u81ea\u52a8\u5173\u95ed\u6587\u4ef6\u3001try-except \u5f02\u5e38\u6355\u83b7\u3001PyCharm \u65ad\u70b9\u8c03\u8bd5&#xff0c;\u9644\u5e26\u5927\u91cf\u53ef\u8fd0\u884c\u5b9e\u6218\u6848\u4f8b&#xff0c;\u9002\u5408\u96f6\u57fa\u7840\u5165\u95e8\u3001\u9762\u8bd5\u590d\u4e60\u3001\u8bfe\u5802\u7b14\u8bb0\u590d\u76d8&#xff0c;\u4ee3\u7801\u5747\u53ef\u76f4\u63a5\u590d\u5236\u8fd0\u884c\u3002<\/p>\n<h3>\u4e8c\u3001Lambda \u533f\u540d\u51fd\u6570&#xff08;\u91cd\u70b9&#xff09;<\/h3>\n<h4>2.1 \u9002\u7528\u573a\u666f<\/h4>\n<p>\u666e\u901a\u51fd\u6570\u9002\u5408\u590d\u7528\u591a\u6b21&#xff1b;lambda \u533f\u540d\u51fd\u6570\u4ec5\u9002\u7528\u4e8e\u51fd\u6570\u53ea\u6709\u4e00\u884c\u4ee3\u7801\u3001\u6709\u8fd4\u56de\u503c\u7684\u573a\u666f&#xff0c;\u5e38\u4f5c\u4e3a\u53c2\u6570\u4f20\u7ed9\u6392\u5e8f\u3001\u9ad8\u9636\u51fd\u6570\u4f7f\u7528&#xff0c;\u7b80\u5316\u4ee3\u7801\u3002<\/p>\n<h4>2.2 \u57fa\u7840\u8bed\u6cd5<\/h4>\n<p># \u8bed\u6cd5<br \/>\n\u53d8\u91cf\u540d &#061; lambda \u53c2\u6570\u5217\u8868: \u8fd4\u56de\u8868\u8fbe\u5f0f<br \/>\n# \u8c03\u7528<br \/>\n\u53d8\u91cf\u540d()<\/p>\n<h4>2.3 \u57fa\u7840\u6848\u4f8b<\/h4>\n<h5>1&#xff09;\u65e0\u53c2 lambda<\/h5>\n<p># \u666e\u901a\u51fd\u6570<br \/>\ndef fun1():<br \/>\n    return 100<br \/>\nprint(fun1())<\/p>\n<p># lambda\u7b80\u5199<br \/>\nlambda_fun1 &#061; lambda: 100<br \/>\nprint(lambda_fun1())<\/p>\n<h5>2&#xff09;\u5e26\u53c2\u6570 lambda<\/h5>\n<p># \u666e\u901a\u51fd\u6570\u6c42\u548c<br \/>\ndef fun2(a, b):<br \/>\n    return a &#043; b<br \/>\nprint(fun2(3,5))<\/p>\n<p># lambda\u6c42\u548c<br \/>\nlambda_fun2 &#061; lambda a,b: a &#043; b<br \/>\nprint(lambda_fun2(4,6))<\/p>\n<p># \u5b57\u7b26\u4e32\u8f6c\u5927\u5199<br \/>\nlambda_fun3 &#061; lambda s: s.upper()<br \/>\nprint(lambda_fun3(&#034;hello python&#034;))<\/p>\n<h5>3&#xff09;\u5e26\u9ed8\u8ba4\u53c2\u6570 lambda<\/h5>\n<p>lambda_fun4 &#061; lambda a, b&#061;100: a &#043; b<br \/>\nprint(lambda_fun4(4))<\/p>\n<h5>4&#xff09;\u7ed3\u5408\u4e09\u76ee\u8fd0\u7b97\u7b26\u5224\u65ad<\/h5>\n<p>Python \u4e09\u76ee\u8bed\u6cd5&#xff1a;\u6ee1\u8db3\u6761\u4ef6\u8fd4\u56de\u503c if \u6761\u4ef6 else \u4e0d\u6ee1\u8db3\u8fd4\u56de\u503c<\/p>\n<p># \u666e\u901a\u51fd\u6570<br \/>\ndef fn8(name):<br \/>\n    return True if name &#061;&#061; &#034;\u5f20\u4e09&#034; else False<br \/>\nprint(fn8(&#034;\u5f20\u4e09&#034;))<\/p>\n<p># lambda\u7b80\u5199<br \/>\nfn9 &#061; lambda name: True if name &#061;&#061; &#034;\u5f20\u4e09&#034; else False<br \/>\nprint(fn9(&#034;\u674e\u56db&#034;))<\/p>\n<h4>2.4 \u5b9e\u6218&#xff1a;\u5217\u8868\u5b57\u5178\u6392\u5e8f&#xff08;\u9ad8\u9891\u8003\u70b9&#xff09;<\/h4>\n<p>\u4f7f\u7528sort(key&#061;lambda)\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219<\/p>\n<p>student_list &#061; [<br \/>\n    {&#034;name&#034;: &#034;\u5f20\u4e09&#034;, &#034;age&#034;: 20, &#034;address&#034;: &#039;\u5357\u4eac&#039;},<br \/>\n    {&#034;name&#034;: &#034;\u674e\u56db&#034;, &#034;age&#034;: 25, &#034;address&#034;: &#039;\u5317\u4eac&#039;},<br \/>\n    {&#034;name&#034;: &#034;\u738b\u4e94&#034;, &#034;age&#034;: 23, &#034;address&#034;: &#039;\u4e0a\u6d77&#039;},<br \/>\n    {&#034;name&#034;: &#034;\u8d75\u516d&#034;, &#034;age&#034;: 19, &#034;address&#034;: &#039;\u5e7f\u5dde&#039;},<br \/>\n    {&#034;name&#034;: &#034;\u7530\u4e03&#034;, &#034;age&#034;: 22, &#034;address&#034;: &#039;\u6df1\u5733&#039;}<br \/>\n]<br \/>\n# \u6839\u636eage\u964d\u5e8f\u6392\u5e8f<br \/>\nstudent_list.sort(key&#061;lambda x: x[&#034;age&#034;], reverse&#061;True)<br \/>\nprint(student_list)<\/p>\n<h3>\u4e09\u3001\u6587\u4ef6 IO \u64cd\u4f5c&#xff08;\u6587\u4ef6\u8bfb\u5199&#xff09;<\/h3>\n<h4>3.1 \u6587\u4ef6\u57fa\u7840\u6982\u5ff5<\/h4>\n<p>\u5185\u5b58\u6570\u636e\u65ad\u7535\u4e22\u5931&#xff0c;\u6587\u4ef6\u6301\u4e45\u5316\u5b58\u50a8\u5728\u78c1\u76d8&#xff1b;\u6587\u4ef6\u5206\u4e3a\u6587\u672c\u6587\u4ef6\u3001\u4e8c\u8fdb\u5236\u6587\u4ef6&#xff08;\u56fe\u7247 \/ \u89c6\u9891 \/ \u97f3\u9891&#xff09;\u3002 \u6587\u4ef6\u64cd\u4f5c\u4e09\u6b65&#xff1a;\u6253\u5f00\u6587\u4ef6 \u2192 \u8bfb\u5199\u64cd\u4f5c \u2192 \u5173\u95ed\u6587\u4ef6<\/p>\n<h4>3.2 open () \u57fa\u7840\u8bed\u6cd5<\/h4>\n<p># \u8bed\u6cd5<br \/>\n\u6587\u4ef6\u5bf9\u8c61 &#061; open(\u6587\u4ef6\u8def\u5f84, \u6253\u5f00\u6a21\u5f0f, encoding&#061;&#034;utf-8&#034;)<br \/>\n# \u64cd\u4f5c<br \/>\n\u6587\u4ef6\u5bf9\u8c61.\u8bfb\u5199\u65b9\u6cd5()<br \/>\n# \u5173\u95ed<br \/>\n\u6587\u4ef6\u5bf9\u8c61.close()<\/p>\n<ul>\n<li>encoding&#061;&#034;utf-8&#034;&#xff1a;\u5fc5\u987b\u52a0&#xff0c;\u89e3\u51b3\u4e2d\u6587\u4e71\u7801<\/li>\n<li>\u6587\u4ef6\u8def\u5f84&#xff1a;\u76f8\u5bf9\u8def\u5f84\u3001\u7edd\u5bf9\u8def\u5f84<\/li>\n<\/ul>\n<h5>\u8def\u5f84\u89c4\u5219<\/h5>\n<table>\n<tr>\u8def\u5f84\u5199\u6cd5\u542b\u4e49<\/tr>\n<tbody>\n<tr>\n<td>.\/a.txt \/ a.txt<\/td>\n<td>\u5f53\u524d\u4ee3\u7801\u540c\u7ea7\u6587\u4ef6<\/td>\n<\/tr>\n<tr>\n<td>..\/a.txt<\/td>\n<td>\u4e0a\u4e00\u7ea7\u76ee\u5f55\u6587\u4ef6<\/td>\n<\/tr>\n<tr>\n<td>\u6587\u4ef6\u5939\/a.txt<\/td>\n<td>\u5f53\u524d\u76ee\u5f55\u4e0b\u7ea7\u6587\u4ef6\u5939\u5185\u6587\u4ef6<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>3.3 \u4e09\u5927\u6838\u5fc3\u6253\u5f00\u6a21\u5f0f r \/w\/a<\/h4>\n<table>\n<tr>\u6a21\u5f0f\u4f5c\u7528\u7279\u70b9<\/tr>\n<tbody>\n<tr>\n<td>r<\/td>\n<td>read \u53ea\u8bfb<\/td>\n<td>\u6587\u4ef6\u4e0d\u5b58\u5728\u76f4\u63a5\u62a5\u9519&#xff1b;\u5149\u6807\u5728\u5f00\u5934&#xff0c;\u4ec5\u53ef\u8bfb<\/td>\n<\/tr>\n<tr>\n<td>w<\/td>\n<td>write \u53ea\u5199<\/td>\n<td>\u6587\u4ef6\u4e0d\u5b58\u5728\u81ea\u52a8\u521b\u5efa&#xff1b;\u6e05\u7a7a\u539f\u6709\u5185\u5bb9\u518d\u5199\u5165<\/td>\n<\/tr>\n<tr>\n<td>a<\/td>\n<td>append \u8ffd\u52a0<\/td>\n<td>\u6587\u4ef6\u4e0d\u5b58\u5728\u81ea\u52a8\u521b\u5efa&#xff1b;\u5149\u6807\u5728\u672b\u5c3e&#xff0c;\u539f\u6709\u5185\u5bb9\u4fdd\u7559<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u62d3\u5c55\u5e26&#043;\/b&#xff1a;<\/p>\n<ul>\n<li>r&#043;\/w&#043;\/a&#043;&#xff1a;\u53ef\u8bfb\u53ef\u5199&#xff0c;\u533a\u522b\u4ec5\u5728\u6587\u4ef6\u5149\u6807\u4f4d\u7f6e<\/li>\n<li>rb\/wb\/ab&#xff1a;\u4e8c\u8fdb\u5236\u6a21\u5f0f&#xff0c;\u8bfb\u5199\u56fe\u7247\u3001\u89c6\u9891\u7b49\u975e\u6587\u672c\u6587\u4ef6<\/li>\n<\/ul>\n<h4>3.4 \u6587\u4ef6\u5199\u5165\u6848\u4f8b w \u6a21\u5f0f<\/h4>\n<p># \u5199\u5165\u6587\u672c&#xff0c;utf-8\u9632\u6b62\u4e2d\u6587\u4e71\u7801<br \/>\nf &#061; open(&#034;.\/data\/a.txt&#034;, &#034;w&#034;, encoding&#061;&#034;utf-8&#034;)<br \/>\nf.write(&#034;\u4eba\u751f\u82e6\u77ed&#xff0c;\u6211\u8981\u5b66Python&#034;)<br \/>\nf.close()<\/p>\n<h4>3.5 \u6587\u4ef6\u8bfb\u53d6\u4e09\u5927\u65b9\u6cd5<\/h4>\n<h5>1&#xff09;read() \/ read(size)<\/h5>\n<p>\u4e00\u6b21\u6027\u8bfb\u53d6\u5168\u90e8\u5185\u5bb9&#xff0c;size \u6307\u5b9a\u8bfb\u53d6\u5b57\u7b26\u957f\u5ea6&#xff0c;\u9002\u5408\u5c0f\u6587\u4ef6<\/p>\n<p>f &#061; open(&#034;.\/data\/a.txt&#034;, &#034;r&#034;, encoding&#061;&#034;utf-8&#034;)<br \/>\n# \u8bfb\u53d6\u5168\u90e8<br \/>\ncontent &#061; f.read()<br \/>\nprint(content)<\/p>\n<p># \u5206\u6bb5\u5faa\u73af\u8bfb\u53d6&#xff0c;\u907f\u514d\u5927\u6587\u4ef6\u5185\u5b58\u6ea2\u51fa<br \/>\nwhile True:<br \/>\n    data &#061; f.read(10)<br \/>\n    if not data:<br \/>\n        break<br \/>\n    print(data, end&#061;&#034;&#034;)<br \/>\nf.close()<\/p>\n<h5>2&#xff09;readlines()<\/h5>\n<p>\u4e00\u6b21\u6027\u8bfb\u53d6\u6240\u6709\u884c&#xff0c;\u8fd4\u56de\u5217\u8868&#xff0c;\u6bcf\u884c\u4f5c\u4e3a\u5217\u8868\u5143\u7d20<\/p>\n<p>f &#061; open(&#034;.\/data\/124.txt&#034;, &#034;r&#034;, encoding&#061;&#034;utf-8&#034;)<br \/>\nline_list &#061; f.readlines()<br \/>\nfor line in line_list:<br \/>\n    print(line, end&#061;&#034;&#034;)<br \/>\nf.close()<\/p>\n<h5>3&#xff09;readline()<\/h5>\n<p>\u5355\u6b21\u8bfb\u53d6\u4e00\u884c&#xff0c;\u5faa\u73af\u8bfb\u53d6\u9002\u5408\u8d85\u5927\u6587\u4ef6<\/p>\n<p>f &#061; open(&#034;.\/data\/124.txt&#034;, &#034;r&#034;, encoding&#061;&#034;utf-8&#034;)<br \/>\nwhile True:<br \/>\n    line &#061; f.readline()<br \/>\n    if not line:<br \/>\n        break<br \/>\n    print(line, end&#061;&#034;&#034;)<br \/>\nf.close()<\/p>\n<h3>\u56db\u3001with open \u81ea\u52a8\u7ba1\u7406\u6587\u4ef6&#xff08;\u63a8\u8350\u5199\u6cd5&#xff09;<\/h3>\n<h4>4.1 \u4f18\u52bf<\/h4>\n<p>\u65e0\u9700\u624b\u52a8\u8c03\u7528close()&#xff0c;\u4ee3\u7801\u6267\u884c\u5b8c\u6bd5\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\u3001\u91ca\u653e\u8d44\u6e90&#xff0c;\u907f\u514d\u5fd8\u8bb0\u5173\u95ed\u5bfc\u81f4\u6587\u4ef6\u5360\u7528\u3002<\/p>\n<h4>4.2 \u8bed\u6cd5<\/h4>\n<p>with open(\u8def\u5f84, \u6a21\u5f0f, encoding&#061;&#034;utf-8&#034;) as \u6587\u4ef6\u522b\u540d:<br \/>\n    \u8bfb\u5199\u64cd\u4f5c\u4ee3\u7801<\/p>\n<h4>4.3 \u5b9e\u6218\u6848\u4f8b<\/h4>\n<h5>\u6848\u4f8b 1&#xff1a;\u6587\u4ef6\u5185\u5bb9\u6309\u884c\u53cd\u8f6c\u5199\u5165\u65b0\u6587\u4ef6<\/h5>\n<p># a.txt\u6bcf\u884c\u53cd\u8f6c\u5b58\u5165b.txt<br \/>\nwith open(&#034;.\/data\/1.txt&#034;, &#034;r&#034;, encoding&#061;&#034;utf-8&#034;) as read_f, open(&#034;.\/data\/2.txt&#034;, &#034;w&#034;, encoding&#061;&#034;utf-8&#034;) as write_f:<br \/>\n    while True:<br \/>\n        line &#061; read_f.readline()<br \/>\n        if not line:<br \/>\n            break<br \/>\n        line &#061; line.replace(&#034;\\\\n&#034;, &#034;&#034;)<br \/>\n        reverse_line &#061; line[::-1]<br \/>\n        write_f.write(reverse_line &#043; &#034;\\\\n&#034;)<\/p>\n<h5>\u6848\u4f8b 2&#xff1a;\u6587\u4ef6\u62f7\u8d1d&#xff08;\u6587\u672c\u6587\u4ef6&#xff09;<\/h5>\n<p># \u590d\u5236a.txt \u5907\u4efd\u4e3a a[\u5907\u4efd].txt<br \/>\nwith open(&#034;.\/data\/a.txt&#034;, &#034;r&#034;, encoding&#061;&#034;utf-8&#034;) as r_f, open(&#034;.\/data\/a[\u5907\u4efd].txt&#034;, &#034;w&#034;, encoding&#061;&#034;utf-8&#034;) as w_f:<br \/>\n    content &#061; r_f.read()<br \/>\n    w_f.write(content)<\/p>\n<h5>\u6848\u4f8b 3&#xff1a;\u4e8c\u8fdb\u5236\u6587\u4ef6\u62f7\u8d1d&#xff08;\u89c6\u9891 \/ \u56fe\u7247&#xff09;<\/h5>\n<p># rb\/wb \u4e8c\u8fdb\u5236\u6a21\u5f0f&#xff0c;\u65e0\u9700encoding<br \/>\nwith open(&#034;test.avi&#034;, &#034;rb&#034;) as src, open(&#034;\u526f\u672c.avi&#034;, &#034;wb&#034;) as dest:<br \/>\n    while True:<br \/>\n        data &#061; src.read(1024*1024) # \u4e00\u6b21\u8bfb\u53d61MB<br \/>\n        if not data:<br \/>\n            break<br \/>\n        dest.write(data)<\/p>\n<h3>\u4e94\u3001\u5f02\u5e38\u5904\u7406 try-except-finally<\/h3>\n<h4>5.1 \u4ec0\u4e48\u662f\u5f02\u5e38<\/h4>\n<p>\u4ee3\u7801\u8fd0\u884c\u51fa\u9519\u65f6\u7a0b\u5e8f\u7ec8\u6b62\u5e76\u629b\u51fa\u62a5\u9519\u4fe1\u606f&#xff0c;\u5373\u4e3a\u5f02\u5e38&#xff1b;\u5f02\u5e38\u6355\u83b7\u53ef\u4ee5\u4fdd\u8bc1\u7a0b\u5e8f\u4e0d\u5d29\u6e83&#xff0c;\u63d0\u5347\u5065\u58ee\u6027\u3002<\/p>\n<h4>5.2 \u5b8c\u6574\u5f02\u5e38\u8bed\u6cd5\u7ed3\u6784<\/h4>\n<p>try:<br \/>\n    # \u53ef\u80fd\u62a5\u9519\u7684\u4ee3\u7801\u5757<br \/>\nexcept \u6307\u5b9a\u5f02\u5e38 as e:<br \/>\n    # \u6355\u83b7\u5bf9\u5e94\u5f02\u5e38\u6267\u884c\u903b\u8f91<br \/>\nexcept Exception as e:<br \/>\n    # \u515c\u5e95\u6355\u83b7\u6240\u6709\u5f02\u5e38&#xff0c;\u6253\u5370\u9519\u8bef\u4fe1\u606f<br \/>\nelse:<br \/>\n    # \u65e0\u5f02\u5e38\u65f6\u6267\u884c<br \/>\nfinally:<br \/>\n    # \u65e0\u8bba\u662f\u5426\u62a5\u9519&#xff0c;\u4e00\u5b9a\u4f1a\u6267\u884c&#xff08;\u5e38\u7528\u4e8e\u5173\u95ed\u6587\u4ef6\u3001\u91ca\u653e\u8d44\u6e90&#xff09;<\/p>\n<h4>5.3 \u57fa\u7840\u6355\u83b7\u6848\u4f8b<\/h4>\n<p># \u6355\u83b7\u672a\u5b9a\u4e49\u53d8\u91cf\u5f02\u5e38<br \/>\ntry:<br \/>\n    print(name)<br \/>\nexcept Exception as e:<br \/>\n    print(&#034;\u9519\u8bef\u4fe1\u606f&#xff1a;&#034;, e)<\/p>\n<h4>5.4 else \u65e0\u5f02\u5e38\u5206\u652f<\/h4>\n<p>try:<br \/>\n    f &#061; open(&#034;python.txt&#034;, &#034;r&#034;, encoding&#061;&#034;utf-8&#034;)<br \/>\nexcept Exception as e:<br \/>\n    print(&#034;\u6587\u4ef6\u6253\u5f00\u5931\u8d25&#xff1a;&#034;, e)<br \/>\nelse:<br \/>\n    # \u6587\u4ef6\u6b63\u5e38\u6253\u5f00\u624d\u6267\u884c<br \/>\n    print(f.readlines())<br \/>\n    f.close()<\/p>\n<h4>5.5 finally \u5fc5\u6267\u884c\u5206\u652f<\/h4>\n<p>try:<br \/>\n    f &#061; open(&#034;python.txt&#034;, &#034;r&#034;, encoding&#061;&#034;utf-8&#034;)<br \/>\nexcept:<br \/>\n    f &#061; open(&#034;python.txt&#034;, &#034;w&#034;, encoding&#061;&#034;utf-8&#034;)<br \/>\nelse:<br \/>\n    print(f.read())<br \/>\nfinally:<br \/>\n    # \u65e0\u8bba\u662f\u5426\u62a5\u9519&#xff0c;\u4e00\u5b9a\u5173\u95ed\u6587\u4ef6<br \/>\n    f.close()<\/p>\n<h4>5.6 \u5b9e\u6218&#xff1a;\u5e26\u5f02\u5e38\u5904\u7406\u7684\u731c\u6570\u5b57\u6e38\u620f<\/h4>\n<p>\u6355\u83b7\u7528\u6237\u975e\u6570\u5b57\u8f93\u5165&#xff0c;\u9632\u6b62\u7a0b\u5e8f\u5d29\u6e83<\/p>\n<p>import random<br \/>\nnum &#061; random.randint(1, 100)<br \/>\nflag &#061; False<br \/>\nfor i in range(3):<br \/>\n    try:<br \/>\n        input_num &#061; int(input(&#034;\u8bf7\u8f93\u51651-100\u6570\u5b57&#xff1a;&#034;))<br \/>\n        if not (1 &lt;&#061; input_num &lt;&#061; 100):<br \/>\n            print(&#034;\u6570\u5b57\u8d85\u51fa\u8303\u56f4&#xff01;&#034;)<br \/>\n            continue<br \/>\n        if input_num &gt; num:<br \/>\n            print(&#034;\u731c\u5927\u4e86&#034;)<br \/>\n        elif input_num &lt; num:<br \/>\n            print(&#034;\u731c\u5c0f\u4e86&#034;)<br \/>\n        else:<br \/>\n            print(&#034;\u606d\u559c\u731c\u5bf9&#xff01;&#034;)<br \/>\n            flag &#061; True<br \/>\n            break<br \/>\n    except:<br \/>\n        print(&#034;\u8f93\u5165\u4e0d\u662f\u6709\u6548\u6570\u5b57&#xff0c;\u8bf7\u91cd\u65b0\u8f93\u5165&#xff01;&#034;)<br \/>\nif not flag:<br \/>\n    print(&#034;3\u6b21\u673a\u4f1a\u7528\u5b8c&#xff0c;\u6e38\u620f\u7ed3\u675f&#034;)<\/p>\n<h3>\u516d\u3001PyCharm Debug \u65ad\u70b9\u8c03\u8bd5<\/h3>\n<p>\u8c03\u8bd5\u662f\u6392\u67e5\u4ee3\u7801 bug \u6838\u5fc3\u6280\u80fd&#xff0c;\u64cd\u4f5c\u6b65\u9aa4&#xff1a;<\/p>\n<li>\u8bbe\u7f6e\u65ad\u70b9&#xff1a;\u70b9\u51fb\u4ee3\u7801\u884c\u53f7\u53f3\u4fa7&#xff0c;\u51fa\u73b0\u7ea2\u8272\u5706\u70b9\u5373\u65ad\u70b9\u751f\u6548&#xff0c;\u518d\u6b21\u70b9\u51fb\u53d6\u6d88<\/li>\n<li>\u542f\u52a8\u8c03\u8bd5&#xff1a;\u53f3\u952eDebug \u6587\u4ef6\u540d\u8fdb\u5165\u8c03\u8bd5\u6a21\u5f0f<\/li>\n<h4>\u5e38\u7528\u5feb\u6377\u952e&#xff08;Windows&#xff09;<\/h4>\n<table>\n<tr>\u5feb\u6377\u952e\u529f\u80fd<\/tr>\n<tbody>\n<tr>\n<td>F8 Step Over<\/td>\n<td>\u6267\u884c\u5f53\u524d\u884c&#xff0c;\u8df3\u8fc7\u51fd\u6570\u5185\u90e8<\/td>\n<\/tr>\n<tr>\n<td>F7 Step Into<\/td>\n<td>\u8fdb\u5165\u5f53\u524d\u884c\u8c03\u7528\u7684\u51fd\u6570\u5185\u90e8<\/td>\n<\/tr>\n<tr>\n<td>Shift&#043;F8 Step Out<\/td>\n<td>\u8df3\u51fa\u5f53\u524d\u51fd\u6570&#xff0c;\u56de\u5230\u8c03\u7528\u4f4d\u7f6e<\/td>\n<\/tr>\n<tr>\n<td>F9 Resume Program<\/td>\n<td>\u8fd0\u884c\u5230\u4e0b\u4e00\u4e2a\u65ad\u70b9<\/td>\n<\/tr>\n<tr>\n<td>Alt&#043;F9 Run to Cursor<\/td>\n<td>\u76f4\u63a5\u8fd0\u884c\u5230\u5149\u6807\u6240\u5728\u884c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u4e03\u3001\u77e5\u8bc6\u70b9\u603b\u7ed3<\/h3>\n<li>Lambda \u533f\u540d\u51fd\u6570&#xff1a;\u5355\u884c\u3001\u5e26\u8fd4\u56de\u503c&#xff0c;\u591a\u7528\u4e8e\u6392\u5e8f key\u3001\u4e34\u65f6\u51fd\u6570\u53c2\u6570&#xff1b;<\/li>\n<li>\u6587\u4ef6\u64cd\u4f5c\u6838\u5fc3&#xff1a;r \u8bfb\u3001w \u6e05\u7a7a\u5199\u3001a \u8ffd\u52a0\u5199&#xff0c;with open\u81ea\u52a8\u5173\u95ed\u6587\u4ef6&#xff1b;<\/li>\n<li>\u6587\u4ef6\u8bfb\u53d6\u4e09\u65b9\u6cd5&#xff1a;read (\u5168\u90e8 \/ \u5206\u6bb5)\u3001readlines (\u884c\u5217\u8868)\u3001readline (\u5355\u884c\u5faa\u73af)&#xff1b;<\/li>\n<li>\u5f02\u5e38\u5904\u7406&#xff1a;try \u653e\u98ce\u9669\u4ee3\u7801&#xff0c;except \u6355\u83b7\u62a5\u9519&#xff0c;else \u65e0\u5f02\u5e38\u6267\u884c&#xff0c;finally \u91ca\u653e\u8d44\u6e90&#xff1b;<\/li>\n<li>Debug \u65ad\u70b9\u8c03\u8bd5&#xff1a;\u719f\u7ec3\u4f7f\u7528 F7\/F8\/F9 \u5feb\u901f\u5b9a\u4f4d\u4ee3\u7801\u903b\u8f91\u9519\u8bef\u3002<\/li>\n<h3>\u516b\u3001\u7ed3\u5c3e<\/h3>\n<p>\u672c\u6587\u8986\u76d6 Python \u6587\u4ef6\u4e0e\u5f02\u5e38\u5168\u90e8\u5b9e\u64cd\u5185\u5bb9&#xff0c;\u6240\u6709\u4ee3\u7801\u5747\u53ef\u76f4\u63a5\u590d\u5236\u8fd0\u884c&#xff0c;\u9002\u5408\u96f6\u57fa\u7840\u81ea\u5b66\u3001\u671f\u672b\u590d\u4e60\u3001\u9762\u8bd5\u5feb\u901f\u590d\u76d8\u3002\u5982\u679c\u5bf9\u4f60\u6709\u5e2e\u52a9&#xff0c;\u6b22\u8fce\u70b9\u8d5e\u6536\u85cf&#xff5e;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u96f6\u57fa\u7840 Day07 \u7b14\u8bb0&#xff5c;Lambda \u533f\u540d\u51fd\u6570\u3001\u6587\u4ef6 IO \u64cd\u4f5c\u3001\u5f02\u5e38\u6355\u83b7\u4e0e Debug \u8c03\u8bd5\u4e00\u3001\u6587\u7ae0\u524d\u8a00\u672c\u7bc7\u6574\u5408 Python \u7b2c 7 \u5929\u6838\u5fc3\u77e5\u8bc6\u70b9&#xff0c;\u5305\u542b\u56db\u5927\u6838\u5fc3\u6a21\u5757&#xff1a;lambda \u533f\u540d\u51fd\u6570\u3001\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\u3001with \u81ea\u52a8\u5173\u95ed\u6587\u4ef6\u3001try-except \u5f02\u5e38\u6355\u83b7\u3001PyCharm \u65ad\u70b9\u8c03\u8bd5&#xff0c;\u9644\u5e26\u5927\u91cf\u53ef\u8fd0\u884c\u5b9e\u6218\u6848\u4f8b&#xff0c;\u9002\u5408\u96f6\u57fa\u7840\u5165\u95e8\u3001\u9762\u8bd5\u590d\u4e60\u3001\u8bfe\u5802\u7b14\u8bb0\u590d\u76d8&#xff0c;\u4ee3\u7801\u5747\u53ef\u76f4\u63a5\u590d\u5236\u8fd0\u884c\u3002\u4e8c\u3001<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[9098,81,50,371],"topic":[],"class_list":["post-89547","post","type-post","status-publish","format-standard","hentry","category-server","tag-it","tag-python","tag-50","tag-371"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>AI\u5927\u6a21\u578b\u5b66\u4e60\u5206\u4eab - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/89547.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"AI\u5927\u6a21\u578b\u5b66\u4e60\u5206\u4eab - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"Python \u96f6\u57fa\u7840 Day07 \u7b14\u8bb0&#xff5c;Lambda \u533f\u540d\u51fd\u6570\u3001\u6587\u4ef6 IO \u64cd\u4f5c\u3001\u5f02\u5e38\u6355\u83b7\u4e0e Debug \u8c03\u8bd5\u4e00\u3001\u6587\u7ae0\u524d\u8a00\u672c\u7bc7\u6574\u5408 Python \u7b2c 7 \u5929\u6838\u5fc3\u77e5\u8bc6\u70b9&#xff0c;\u5305\u542b\u56db\u5927\u6838\u5fc3\u6a21\u5757&#xff1a;lambda \u533f\u540d\u51fd\u6570\u3001\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\u3001with \u81ea\u52a8\u5173\u95ed\u6587\u4ef6\u3001try-except \u5f02\u5e38\u6355\u83b7\u3001PyCharm \u65ad\u70b9\u8c03\u8bd5&#xff0c;\u9644\u5e26\u5927\u91cf\u53ef\u8fd0\u884c\u5b9e\u6218\u6848\u4f8b&#xff0c;\u9002\u5408\u96f6\u57fa\u7840\u5165\u95e8\u3001\u9762\u8bd5\u590d\u4e60\u3001\u8bfe\u5802\u7b14\u8bb0\u590d\u76d8&#xff0c;\u4ee3\u7801\u5747\u53ef\u76f4\u63a5\u590d\u5236\u8fd0\u884c\u3002\u4e8c\u3001\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/89547.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-08-03T00:40:40+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/89547.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/89547.html\",\"name\":\"AI\u5927\u6a21\u578b\u5b66\u4e60\u5206\u4eab - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-08-03T00:40:40+00:00\",\"dateModified\":\"2026-08-03T00:40:40+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/89547.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/89547.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/89547.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"AI\u5927\u6a21\u578b\u5b66\u4e60\u5206\u4eab\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"AI\u5927\u6a21\u578b\u5b66\u4e60\u5206\u4eab - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/89547.html","og_locale":"zh_CN","og_type":"article","og_title":"AI\u5927\u6a21\u578b\u5b66\u4e60\u5206\u4eab - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"Python \u96f6\u57fa\u7840 Day07 \u7b14\u8bb0&#xff5c;Lambda \u533f\u540d\u51fd\u6570\u3001\u6587\u4ef6 IO \u64cd\u4f5c\u3001\u5f02\u5e38\u6355\u83b7\u4e0e Debug \u8c03\u8bd5\u4e00\u3001\u6587\u7ae0\u524d\u8a00\u672c\u7bc7\u6574\u5408 Python \u7b2c 7 \u5929\u6838\u5fc3\u77e5\u8bc6\u70b9&#xff0c;\u5305\u542b\u56db\u5927\u6838\u5fc3\u6a21\u5757&#xff1a;lambda \u533f\u540d\u51fd\u6570\u3001\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\u3001with \u81ea\u52a8\u5173\u95ed\u6587\u4ef6\u3001try-except \u5f02\u5e38\u6355\u83b7\u3001PyCharm \u65ad\u70b9\u8c03\u8bd5&#xff0c;\u9644\u5e26\u5927\u91cf\u53ef\u8fd0\u884c\u5b9e\u6218\u6848\u4f8b&#xff0c;\u9002\u5408\u96f6\u57fa\u7840\u5165\u95e8\u3001\u9762\u8bd5\u590d\u4e60\u3001\u8bfe\u5802\u7b14\u8bb0\u590d\u76d8&#xff0c;\u4ee3\u7801\u5747\u53ef\u76f4\u63a5\u590d\u5236\u8fd0\u884c\u3002\u4e8c\u3001","og_url":"https:\/\/www.wsisp.com\/helps\/89547.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-08-03T00:40:40+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/89547.html","url":"https:\/\/www.wsisp.com\/helps\/89547.html","name":"AI\u5927\u6a21\u578b\u5b66\u4e60\u5206\u4eab - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-08-03T00:40:40+00:00","dateModified":"2026-08-03T00:40:40+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/89547.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/89547.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/89547.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"AI\u5927\u6a21\u578b\u5b66\u4e60\u5206\u4eab"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/89547","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=89547"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/89547\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=89547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=89547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=89547"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=89547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}