﻿{"id":1472,"date":"2020-06-10T12:00:05","date_gmt":"2020-06-10T04:00:05","guid":{"rendered":"https:\/\/www.tansoo.cn\/?p=1472"},"modified":"2020-06-10T12:00:05","modified_gmt":"2020-06-10T04:00:05","slug":"phpbb-30","status":"publish","type":"post","link":"https:\/\/www.tansoo.cn\/?p=1472","title":{"rendered":"python\u4e2d\u201cfor &#8211; else\u201d\u7684\u7528\u6cd5"},"content":{"rendered":"<p><strong class=\"text-strong\"><span style=\"font-size:150%;line-height:116%\">For &#8211; Else<\/span><\/strong><br \/>\n\u5faa\u73af\u662f\u4efb\u4f55\u8bed\u8a00\u7684\u4e00\u4e2a\u5fc5\u5907\u8981\u7d20\u3002\u540c\u6837\u5730\uff0cfor\u5faa\u73af\u5c31\u662fPython\u7684\u4e00\u4e2a\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002\u7136\u800c\u8fd8\u6709\u4e00\u4e9b\u4e1c\u897f\u662f\u521d\u5b66\u8005\u5e76\u4e0d\u77e5\u9053\u7684\u3002\u6211\u4eec\u5c06\u4e00\u4e2a\u4e2a\u8ba8\u8bba\u4e00\u4e0b\u3002<\/p>\n<p>\u6211\u4eec\u5148\u4ece\u5df2\u7ecf\u77e5\u9053\u7684\u5f00\u59cb\u3002\u6211\u4eec\u77e5\u9053\u53ef\u4ee5\u50cf\u8fd9\u6837\u4f7f\u7528for\u5faa\u73af\uff1a<\/p>\n<div class=\"codebox_plus_wrap\">\n<div class=\"codebox_plus_header\"><strong>Code: <\/strong><a href=\"#\" onclick=\"codebox_plus_select(this, 1); return false;\">[\u5168\u9009]<\/a>&nbsp;<a href=\"#\" onclick=\"codebox_plus_toggle(this, 1); return false;\">[Expand\/Collapse]<\/a><\/div>\n<div>\n<div style=\"display: inline;\">\n<div class=\"python\" style=\"font-family:monospace;\">\n<ol>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">fruits <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: black;\">&#91;<\/span><span style=\"color: #483d8b;\">&#8216;apple&#8217;<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #483d8b;\">&#8216;banana&#8217;<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #483d8b;\">&#8216;mango&#8217;<\/span><span style=\"color: black;\">&#93;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #ff7700;font-weight:bold;\">for<\/span> fruit <span style=\"color: #ff7700;font-weight:bold;\">in<\/span> fruits:<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; <span style=\"color: #ff7700;font-weight:bold;\">print<\/span><span style=\"color: black;\">&#40;<\/span>fruit.<span style=\"color: black;\">capitalize<\/span><span style=\"color: black;\">&#40;<\/span><span style=\"color: black;\">&#41;<\/span><span style=\"color: black;\">&#41;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp;<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #808080; font-style: italic;\"># Output: Apple<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #808080; font-style: italic;\"># &nbsp; &nbsp; &nbsp; &nbsp; Banana<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #808080; font-style: italic;\"># &nbsp; &nbsp; &nbsp; &nbsp; Mango<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"codebox_plus_footer\">GeSHi &copy; Codebox Plus Extension<\/div>\n<\/div>\n<p>\u8fd9\u662f\u4e00\u4e2afor\u5faa\u73af\u975e\u5e38\u57fa\u7840\u7684\u7ed3\u6784\u3002\u73b0\u5728\u6211\u4eec\u7ee7\u7eed\u770b\u770b\uff0cPython\u7684for\u5faa\u73af\u7684\u4e00\u4e9b\u9c9c\u4e3a\u4eba\u6240\u77e5\u7684\u7279\u6027\u3002 <\/p>\n<p><strong class=\"text-strong\"><span style=\"font-size:150%;line-height:116%\">else\u4ece\u53e5<\/span><\/strong><br \/>\nfor\u5faa\u73af\u8fd8\u6709\u4e00\u4e2aelse\u4ece\u53e5\uff0c\u6211\u4eec\u5927\u591a\u6570\u4eba\u5e76\u4e0d\u719f\u6089\u3002\u8fd9\u4e2aelse\u4ece\u53e5\u4f1a\u5728\u5faa\u73af\u6b63\u5e38\u7ed3\u675f\u65f6\u6267\u884c\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5faa\u73af\u6ca1\u6709\u9047\u5230\u4efb\u4f55break. \u4e00\u65e6\u4f60\u638c\u63e1\u4e86\u4f55\u65f6\u4f55\u5730\u4f7f\u7528\u5b83\uff0c\u5b83\u771f\u7684\u4f1a\u975e\u5e38\u6709\u7528\u3002\u6211\u81ea\u5df1\u5bf9\u5b83\u771f\u662f\u76f8\u89c1\u6068\u665a\u3002<\/p>\n<p>\u6709\u4e2a\u5e38\u89c1\u7684\u6784\u9020\u662f\u8dd1\u4e00\u4e2a\u5faa\u73af\uff0c\u5e76\u67e5\u627e\u4e00\u4e2a\u5143\u7d20\u3002\u5982\u679c\u8fd9\u4e2a\u5143\u7d20\u88ab\u627e\u5230\u4e86\uff0c\u6211\u4eec\u4f7f\u7528break\u6765\u4e2d\u65ad\u8fd9\u4e2a\u5faa\u73af\u3002\u6709\u4e24\u4e2a\u573a\u666f\u4f1a\u8ba9\u5faa\u73af\u505c\u4e0b\u6765\u3002 &#8211; \u7b2c\u4e00\u4e2a\u662f\u5f53\u4e00\u4e2a\u5143\u7d20\u88ab\u627e\u5230\uff0cbreak\u88ab\u89e6\u53d1\u3002 &#8211; \u7b2c\u4e8c\u4e2a\u573a\u666f\u662f\u5faa\u73af\u7ed3\u675f\u3002<\/p>\n<p>\u73b0\u5728\u6211\u4eec\u4e5f\u8bb8\u60f3\u77e5\u9053\u5176\u4e2d\u54ea\u4e00\u4e2a\uff0c\u624d\u662f\u5bfc\u81f4\u5faa\u73af\u5b8c\u6210\u7684\u539f\u56e0\u3002\u4e00\u4e2a\u65b9\u6cd5\u662f\u5148\u8bbe\u7f6e\u4e00\u4e2a\u6807\u8bb0\uff0c\u7136\u540e\u5728\u5faa\u73af\u7ed3\u675f\u65f6\u6253\u4e0a\u6807\u8bb0\u3002\u53e6\u4e00\u4e2a\u662f\u4f7f\u7528else\u4ece\u53e5\u3002<\/p>\n<p>\u8fd9\u5c31\u662ffor\/else\u5faa\u73af\u7684\u57fa\u672c\u7ed3\u6784\uff1a<\/p>\n<div class=\"codebox_plus_wrap\">\n<div class=\"codebox_plus_header\"><strong>Code: <\/strong><a href=\"#\" onclick=\"codebox_plus_select(this, 1); return false;\">[\u5168\u9009]<\/a>&nbsp;<a href=\"#\" onclick=\"codebox_plus_toggle(this, 1); return false;\">[Expand\/Collapse]<\/a><\/div>\n<div>\n<div style=\"display: inline;\">\n<div class=\"python\" style=\"font-family:monospace;\">\n<ol>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #ff7700;font-weight:bold;\">for<\/span> item <span style=\"color: #ff7700;font-weight:bold;\">in<\/span> container:<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; <span style=\"color: #ff7700;font-weight:bold;\">if<\/span> search_something<span style=\"color: black;\">&#40;<\/span>item<span style=\"color: black;\">&#41;<\/span>:<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #808080; font-style: italic;\"># Found it!<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; &nbsp; &nbsp; process<span style=\"color: black;\">&#40;<\/span>item<span style=\"color: black;\">&#41;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #ff7700;font-weight:bold;\">break<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #ff7700;font-weight:bold;\">else<\/span>:<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; <span style=\"color: #808080; font-style: italic;\"># Didn&#8217;t find anything..<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; not_found_in_container<span style=\"color: black;\">&#40;<\/span><span style=\"color: black;\">&#41;<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"codebox_plus_footer\">GeSHi &copy; Codebox Plus Extension<\/div>\n<\/div>\n<p>\u8003\u8651\u4e0b\u8fd9\u4e2a\u7b80\u5355\u7684\u6848\u4f8b\uff0c\u5b83\u662f\u6211\u4ece\u5b98\u65b9\u6587\u6863\u91cc\u62ff\u6765\u7684\uff1a<\/p>\n<div class=\"codebox_plus_wrap\">\n<div class=\"codebox_plus_header\"><strong>Code: <\/strong><a href=\"#\" onclick=\"codebox_plus_select(this, 1); return false;\">[\u5168\u9009]<\/a>&nbsp;<a href=\"#\" onclick=\"codebox_plus_toggle(this, 1); return false;\">[Expand\/Collapse]<\/a><\/div>\n<div>\n<div style=\"display: inline;\">\n<div class=\"python\" style=\"font-family:monospace;\">\n<ol>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #ff7700;font-weight:bold;\">for<\/span> n <span style=\"color: #ff7700;font-weight:bold;\">in<\/span> <span style=\"color: #008000;\">range<\/span><span style=\"color: black;\">&#40;<\/span><span style=\"color: #ff4500;\">2<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff4500;\">10<\/span><span style=\"color: black;\">&#41;<\/span>:<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; <span style=\"color: #ff7700;font-weight:bold;\">for<\/span> x <span style=\"color: #ff7700;font-weight:bold;\">in<\/span> <span style=\"color: #008000;\">range<\/span><span style=\"color: black;\">&#40;<\/span><span style=\"color: #ff4500;\">2<\/span><span style=\"color: #66cc66;\">,<\/span> n<span style=\"color: black;\">&#41;<\/span>:<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #ff7700;font-weight:bold;\">if<\/span> n % x <span style=\"color: #66cc66;\">==<\/span> <span style=\"color: #ff4500;\">0<\/span>:<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #ff7700;font-weight:bold;\">print<\/span><span style=\"color: black;\">&#40;<\/span>n<span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #483d8b;\">&#8216;equals&#8217;<\/span><span style=\"color: #66cc66;\">,<\/span> x<span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #483d8b;\">&#8216;*&#8217;<\/span><span style=\"color: #66cc66;\">,<\/span> n\/x<span style=\"color: black;\">&#41;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #ff7700;font-weight:bold;\">break<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"codebox_plus_footer\">GeSHi &copy; Codebox Plus Extension<\/div>\n<\/div>\n<p>\u5b83\u4f1a\u627e\u51fa2\u523010\u4e4b\u95f4\u7684\u6570\u5b57\u7684\u56e0\u5b50\u3002\u73b0\u5728\u662f\u8da3\u5473\u73af\u8282\u4e86\u3002\u6211\u4eec\u53ef\u4ee5\u52a0\u4e0a\u4e00\u4e2a\u9644\u52a0\u7684else\u8bed\u53e5\u5757\uff0c\u6765\u6293\u4f4f\u8d28\u6570\uff0c\u5e76\u4e14\u544a\u8bc9\u6211\u4eec\uff1a<\/p>\n<div class=\"codebox_plus_wrap\">\n<div class=\"codebox_plus_header\"><strong>Code: <\/strong><a href=\"#\" onclick=\"codebox_plus_select(this, 1); return false;\">[\u5168\u9009]<\/a>&nbsp;<a href=\"#\" onclick=\"codebox_plus_toggle(this, 1); return false;\">[Expand\/Collapse]<\/a><\/div>\n<div>\n<div style=\"display: inline;\">\n<div class=\"python\" style=\"font-family:monospace;\">\n<ol>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #ff7700;font-weight:bold;\">for<\/span> n <span style=\"color: #ff7700;font-weight:bold;\">in<\/span> <span style=\"color: #008000;\">range<\/span><span style=\"color: black;\">&#40;<\/span><span style=\"color: #ff4500;\">2<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff4500;\">10<\/span><span style=\"color: black;\">&#41;<\/span>:<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; <span style=\"color: #ff7700;font-weight:bold;\">for<\/span> x <span style=\"color: #ff7700;font-weight:bold;\">in<\/span> <span style=\"color: #008000;\">range<\/span><span style=\"color: black;\">&#40;<\/span><span style=\"color: #ff4500;\">2<\/span><span style=\"color: #66cc66;\">,<\/span> n<span style=\"color: black;\">&#41;<\/span>:<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #ff7700;font-weight:bold;\">if<\/span> n % x <span style=\"color: #66cc66;\">==<\/span> <span style=\"color: #ff4500;\">0<\/span>:<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #ff7700;font-weight:bold;\">print<\/span><span style=\"color: black;\">&#40;<\/span> n<span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #483d8b;\">&#8216;equals&#8217;<\/span><span style=\"color: #66cc66;\">,<\/span> x<span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #483d8b;\">&#8216;*&#8217;<\/span><span style=\"color: #66cc66;\">,<\/span> n\/x<span style=\"color: black;\">&#41;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #ff7700;font-weight:bold;\">break<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; <span style=\"color: #ff7700;font-weight:bold;\">else<\/span>:<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #808080; font-style: italic;\"># loop fell through without finding a factor<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #ff7700;font-weight:bold;\">print<\/span><span style=\"color: black;\">&#40;<\/span>n<span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #483d8b;\">&#8216;is a prime number&#8217;<\/span><span style=\"color: black;\">&#41;<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"codebox_plus_footer\">GeSHi &copy; Codebox Plus Extension<\/div>\n<\/div>\n<p><small>\u539f\u6587\u4f5c\u8005: BG6RSH | <a href=\"https:\/\/bbs.tansoo.cn\/viewtopic.php?t=30\" target=\"_blank\" rel=\"noopener\">\u67e5\u770b\u539f\u5e16<\/a><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>For &#8211; Else \u5faa\u73af\u662f\u4efb\u4f55\u8bed\u8a00\u7684\u4e00\u4e2a\u5fc5\u5907\u8981\u7d20\u3002\u540c\u6837\u5730\uff0cfor\u5faa &hellip; <a href=\"https:\/\/www.tansoo.cn\/?p=1472\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[],"class_list":["post-1472","post","type-post","status-publish","format-standard","hentry","category-105"],"_links":{"self":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1472","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1472"}],"version-history":[{"count":0,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1472\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}