{"id":1674,"date":"2020-06-22T06:37:16","date_gmt":"2020-06-21T22:37:16","guid":{"rendered":"http:\/\/39.97.231.10\/?p=1674"},"modified":"2020-06-22T06:37:16","modified_gmt":"2020-06-21T22:37:16","slug":"%e5%9c%a8-python-%e4%b8%ad%e6%8a%9b%e5%87%ba%e5%bc%82%e5%b8%b8%e4%bb%a5%e5%8f%8a%e7%9b%b8%e5%85%b3%e5%ba%94%e7%94%a8%e5%9c%ba%e6%99%af","status":"publish","type":"post","link":"http:\/\/www.weijing.co\/?p=1674","title":{"rendered":"\u5728 Python \u4e2d\u629b\u51fa\u5f02\u5e38\u4ee5\u53ca\u76f8\u5173\u5e94\u7528\u573a\u666f"},"content":{"rendered":"<div>\n<div>\n<div>\n<div>\u5f53 Python \u8bd5\u56fe\u6267\u884c\u65e0\u6548\u4ee3\u7801\u65f6\uff0c\u5c31\u4f1a\u629b\u51fa\u5f02\u5e38\u3002<\/div>\n<\/div>\n<\/div>\n<\/div>\n<article>\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528 raise \u8bed\u53e5\u629b\u51fa\u5f02\u5e38\u3002\u5728\u4ee3\u7801\u4e2d\uff0c raise \u8bed\u53e5\u5305\u542b\u4ee5\u4e0b\u90e8\u5206\uff1a<\/p>\n<ul>\n<li>raise \u5173\u952e\u5b57\uff1b<\/li>\n<li>\u8c03\u7528 Exception \u51fd\u6570\uff0c\u540c\u65f6\u4f20\u5165\u51fa\u9519\u4fe1\u606f\u63cf\u8ff0\u3002<\/li>\n<\/ul>\n<div>\n<pre>raise Exception(\\'\u629b\u51fa\u5f02\u5e38\\')\n<\/pre>\n<\/div>\n<div>\n<div>\n<div><\/div>\n<div data-width=\\\"336\\\" data-height=\\\"95\\\"><img src=\\\"https:\/\/upload-images.jianshu.io\/upload_images\/3386108-0f1314ee8823f920.png?imageMogr2\/auto-orient\/strip|imageView2\/2\/w\/336\/format\/webp\\\" data-original-src=\\\"\/\/upload-images.jianshu.io\/upload_images\/3386108-0f1314ee8823f920.png\\\" data-original-width=\\\"336\\\" data-original-height=\\\"95\\\" data-original-format=\\\"image\/png\\\" data-original-filesize=\\\"7923\\\" data-image-index=\\\"0\\\" \/><\/div>\n<\/div>\n<\/div>\n<p>Al\u2002Sweigart \u5199\u8fc7\u8fd9\u6837\u4e00\u4e2a\u793a\u4f8b\uff0c\u5f88\u597d\u5730\u8be0\u91ca\u4e86\u629b\u51fa\u5f02\u5e38\u7528\u6cd5\u3002Sweigart \u5b9a\u4e49\u4e86\u4e00\u4e2a box_print() \u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u3001\u4e00\u4e2a\u5bbd\u5ea6\u548c\u4e00\u4e2a\u9ad8\u5ea6\u4f5c\u4e3a\u5165\u53c2\u3002\u7136\u540e\u6309\u7167\u6307\u5b9a\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\uff0c\u4f7f\u7528\u8be5\u5b57\u7b26\u521b\u5efa\u4e86\u4e00\u4e2a\u5c0f\u76d2\u5b50\u7684\u56fe\u50cf\u3002\u6700\u540e\u628a\u8fd9\u4e2a\u76d2\u5b50\u6253\u5370\u5230\u5c4f\u5e55\u4e0a\u3002<\/p>\n<div>\n<pre>def box_print(symbol, width, height):\n    if len(symbol) != 1:\n        raise Exception(\\'Symbol must be a single character string.\\')\n    if width &lt;= 2:\n        raise Exception(\\'Width must be greater than 2.\\')\n    if height &lt;= 2:\n        raise Exception(\\'Height must be greater than 2.\\')\n    print(symbol * width)\n    for i in range(height - 2):\n        print(symbol + (\\' \\' * (width - 2)) + symbol)\n    print(symbol * width)\nfor sym, w, h in ((\\'*\\', 4, 4), (\\'O\\', 20, 5), (\\'x\\', 1, 3), (\\'ZZ\\', 3, 3)):\n    try:\n        box_print(sym, w, h)\n    except Exception as err:\n        print(\\'An exception happened:\\' + str(err))\n<\/pre>\n<\/div>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<div>\n<div>\n<div><\/div>\n<div data-width=\\\"660\\\" data-height=\\\"276\\\"><img src=\\\"https:\/\/upload-images.jianshu.io\/upload_images\/3386108-58118dbf4348b214.png?imageMogr2\/auto-orient\/strip|imageView2\/2\/w\/660\/format\/webp\\\" data-original-src=\\\"\/\/upload-images.jianshu.io\/upload_images\/3386108-58118dbf4348b214.png\\\" data-original-width=\\\"660\\\" data-original-height=\\\"276\\\" data-original-format=\\\"image\/png\\\" data-original-filesize=\\\"12839\\\" data-image-index=\\\"1\\\" \/><\/div>\n<\/div>\n<\/div>\n<p>\u8be5\u51fd\u6570\u5e0c\u671b\u8f93\u5165\u7684\u5b57\u7b26\u957f\u5ea6\u5927\u4e8e 0\uff0c\u5e76\u4e14\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u8981\u5927\u4e8e 2\uff0c\u5426\u5219\u629b\u51fa\u5f02\u5e38\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u8fd8\u4f7f\u7528\u4e86\u4ee5\u4e0b\u6280\u5de7\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 * \u7b26\u53f7\u6765\u5b9e\u73b0\u590d\u5236\u591a\u4e2a\u7a7a\u683c\u3002<\/li>\n<li>\u4f7f\u7528 for in \u65b9\u6cd5\u6765\u521d\u59cb\u5316\u5143\u7ec4\u6570\u636e\u3002<\/li>\n<li>\u8fd8\u4f7f\u7528\u4e86 except \u8bed\u53e5\u7684 except Exception as err \u5f62\u5f0f\uff0c\u5982\u679c box_print() \u8fd4\u56de\u4e00\u4e2a Exception \u5bf9\u8c61\uff0c\u90a3\u4e48\u8fd9\u6761\u8bed\u53e5\u5c31\u4f1a\u5c06\u5b83\u4fdd\u5b58\u5728\u540d\u4e3a err \u7684\u53d8\u91cf\u4e2d\u3002<\/li>\n<\/ul>\n<hr \/>\n<p>\u4f7f\u7528 try \u548c except \u8bed\u53e5\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u5bb9\u4f7f\u7528\u66f4\u4f18\u96c5\u7684\u65b9\u5f0f\u6765\u5904\u7406\u9519\u8bef\u3002<br \/>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53 Python \u8bd5\u56fe\u6267\u884c\u65e0\u6548\u4ee3\u7801\u65f6\uff0c\u5c31\u4f1a\u629b\u51fa\u5f02\u5e38\u3002 \u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528 raise \u8bed\u53e5\u629b\u51fa\u5f02\u5e38\u3002\u5728\u4ee3\u7801\u4e2d\uff0c r &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.weijing.co\/?p=1674\"> <span class=\"screen-reader-text\">\u5728 Python \u4e2d\u629b\u51fa\u5f02\u5e38\u4ee5\u53ca\u76f8\u5173\u5e94\u7528\u573a\u666f<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[106,126],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"mailpoet_newsletter_max":false,"woocommerce_thumbnail":false,"woocommerce_single":false,"woocommerce_gallery_thumbnail":false,"shop_catalog":false,"shop_single":false,"shop_thumbnail":false},"uagb_author_info":{"display_name":"weijing","author_link":"http:\/\/www.weijing.co\/?author=4"},"uagb_comment_info":0,"uagb_excerpt":"\u5f53 Python \u8bd5\u56fe\u6267\u884c\u65e0\u6548\u4ee3\u7801\u65f6\uff0c\u5c31\u4f1a\u629b\u51fa\u5f02\u5e38\u3002 \u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528 raise \u8bed\u53e5\u629b\u51fa\u5f02\u5e38\u3002\u5728\u4ee3\u7801\u4e2d\uff0c r&hellip;","_links":{"self":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1674"}],"collection":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1674"}],"version-history":[{"count":0,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1674\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1674"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}