﻿{"id":1465,"date":"2020-06-10T06:58:30","date_gmt":"2020-06-09T22:58:30","guid":{"rendered":"https:\/\/www.tansoo.cn\/?p=1465"},"modified":"2020-06-10T06:58:30","modified_gmt":"2020-06-09T22:58:30","slug":"phpbb-28","status":"publish","type":"post","link":"https:\/\/www.tansoo.cn\/?p=1465","title":{"rendered":"python\u4e09\u5143\u8fd0\u7b97\u7b26\u201cif &#8211; else\u201d\u7528\u6cd5"},"content":{"rendered":"<p>\u4e09\u5143\u8fd0\u7b97\u7b26\u901a\u5e38\u5728Python\u91cc\u88ab\u79f0\u4e3a\u6761\u4ef6\u8868\u8fbe\u5f0f\uff0c\u8fd9\u4e9b\u8868\u8fbe\u5f0f\u57fa\u4e8e\u771f(true)\/\u5047(not)\u7684\u6761\u4ef6\u5224\u65ad\uff0c\u5728Python 2.4\u4ee5\u4e0a\u624d\u6709\u4e86\u4e09\u5143\u64cd\u4f5c\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f2a\u4ee3\u7801\u548c\u4f8b\u5b50\uff1a<\/p>\n<p>\u4f2a\u4ee3\u7801:<\/p>\n<p>#\u5982\u679c\u6761\u4ef6\u4e3a\u771f\uff0c\u8fd4\u56de\u771f \u5426\u5219\u8fd4\u56de\u5047<\/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;\">condition_is_true <span style=\"color: #ff7700;font-weight:bold;\">if<\/span> condition <span style=\"color: #ff7700;font-weight:bold;\">else<\/span> condition_is_false<\/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>\u4f8b\u5b50:<\/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;\">is_fat <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #008000;\">True<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">state <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #483d8b;\">&quot;fat&quot;<\/span> <span style=\"color: #ff7700;font-weight:bold;\">if<\/span> is_fat <span style=\"color: #ff7700;font-weight:bold;\">else<\/span> <span style=\"color: #483d8b;\">&quot;not fat&quot;<\/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\u5141\u8bb8\u7528\u7b80\u5355\u7684\u4e00\u884c\u5feb\u901f\u5224\u65ad\uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u590d\u6742\u7684\u591a\u884cif\u8bed\u53e5\u3002 \u8fd9\u5728\u5927\u591a\u6570\u65f6\u5019\u975e\u5e38\u6709\u7528\uff0c\u800c\u4e14\u53ef\u4ee5\u4f7f\u4ee3\u7801\u7b80\u5355\u53ef\u7ef4\u62a4\u3002<\/p>\n<p>\u53e6\u4e00\u4e2a\u6666\u6da9\u4e00\u70b9\u7684\u7528\u6cd5\u6bd4\u8f83\u5c11\u89c1\uff0c\u5b83\u4f7f\u7528\u4e86\u5143\u7ec4\uff0c\u8bf7\u7ee7\u7eed\u770b\uff1a<\/p>\n<p>\u4f2a\u4ee3\u7801:<\/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: #808080; font-style: italic;\">#(\u8fd4\u56de\u5047\uff0c\u8fd4\u56de\u771f)[\u771f\u6216\u5047]<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: black;\">&#40;<\/span>if_test_is_false<span style=\"color: #66cc66;\">,<\/span> if_test_is_true<span style=\"color: black;\">&#41;<\/span><span style=\"color: black;\">&#91;<\/span><span style=\"color: #dc143c;\">test<\/span><span style=\"color: black;\">&#93;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">\u4f8b\u5b50:<\/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;\">fat <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #008000;\">True<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">fitness <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: black;\">&#40;<\/span><span style=\"color: #483d8b;\">&quot;skinny&quot;<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #483d8b;\">&quot;fat&quot;<\/span><span style=\"color: black;\">&#41;<\/span><span style=\"color: black;\">&#91;<\/span>fat<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;\">print<\/span><span style=\"color: black;\">&#40;<\/span><span style=\"color: #483d8b;\">&quot;Ali is &quot;<\/span><span style=\"color: #66cc66;\">,<\/span> fitness<span style=\"color: black;\">&#41;<\/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;\">#\u8f93\u51fa: Ali is fat<\/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\u4e4b\u6240\u4ee5\u80fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u662f\u56e0\u4e3a\u5728Python\u4e2d\uff0cTrue\u7b49\u4e8e1\uff0c\u800cFalse\u7b49\u4e8e0\uff0c\u8fd9\u5c31\u76f8\u5f53\u4e8e\u5728\u5143\u7ec4\u4e2d\u4f7f\u75280\u548c1\u6765\u9009\u53d6\u6570\u636e\u3002<\/p>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u6ca1\u6709\u88ab\u5e7f\u6cdb\u4f7f\u7528\uff0c\u800c\u4e14Python\u73a9\u5bb6\u4e00\u822c\u4e0d\u559c\u6b22\u90a3\u6837\uff0c\u56e0\u4e3a\u6ca1\u6709Python\u5473\u513f(Pythonic)\u3002\u8fd9\u6837\u7684\u7528\u6cd5\u5f88\u5bb9\u6613\u628a\u771f\u6b63\u7684\u6570\u636e\u4e0etrue\/false\u5f04\u6df7\u3002<\/p>\n<p>\u53e6\u5916\u4e00\u4e2a\u4e0d\u4f7f\u7528\u5143\u7ec4\u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u7f18\u6545\u662f\u56e0\u4e3a\u5728\u5143\u7ec4\u4e2d\u4f1a\u628a\u4e24\u4e2a\u6761\u4ef6\u90fd\u6267\u884c\uff0c\u800c  if-else \u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\u4e0d\u4f1a\u8fd9\u6837\u3002<\/p>\n<p>\u4f8b\u5982:<\/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;\">condition <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #008000;\">True<\/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;\">print<\/span><span style=\"color: black;\">&#40;<\/span><span style=\"color: #ff4500;\">2<\/span> <span style=\"color: #ff7700;font-weight:bold;\">if<\/span> condition <span style=\"color: #ff7700;font-weight:bold;\">else<\/span> <span style=\"color: #ff4500;\">1<\/span>\/<span style=\"color: #ff4500;\">0<\/span><span style=\"color: black;\">&#41;<\/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;\">#\u8f93\u51fa: 2<\/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: #ff7700;font-weight:bold;\">print<\/span><span style=\"color: black;\">&#40;<\/span><span style=\"color: black;\">&#40;<\/span><span style=\"color: #ff4500;\">1<\/span>\/<span style=\"color: #ff4500;\">0<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff4500;\">2<\/span><span style=\"color: black;\">&#41;<\/span><span style=\"color: black;\">&#91;<\/span>condition<span style=\"color: black;\">&#93;<\/span><span style=\"color: black;\">&#41;<\/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;\">#\u8f93\u51faZeroDivisionError\u5f02\u5e38<\/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\u56e0\u4e3a\u5728\u5143\u7ec4\u4e2d\u662f\u5148\u5efa\u6570\u636e\uff0c\u7136\u540e\u7528True(1)\/False(0)\u6765\u7d22\u5f15\u5230\u6570\u636e\u3002 \u800cif-else\u6761\u4ef6\u8868\u8fbe\u5f0f\u9075\u5faa\u666e\u901a\u7684if-else\u903b\u8f91\u6811\uff0c \u56e0\u6b64\uff0c\u5982\u679c\u903b\u8f91\u4e2d\u7684\u6761\u4ef6\u5f02\u5e38\uff0c\u6216\u8005\u662f\u91cd\u8ba1\u7b97\u578b\uff08\u8ba1\u7b97\u8f83\u4e45\uff09\u7684\u60c5\u51b5\u4e0b\uff0c\u6700\u597d\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u5143\u7ec4\u6761\u4ef6\u8868\u8fbe\u5f0f\u3002<\/p>\n<p><small>\u539f\u6587\u4f5c\u8005: BG6RSH | <a href=\"https:\/\/bbs.tansoo.cn\/viewtopic.php?t=28\" target=\"_blank\" rel=\"noopener\">\u67e5\u770b\u539f\u5e16<\/a><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e09\u5143\u8fd0\u7b97\u7b26\u901a\u5e38\u5728Python\u91cc\u88ab\u79f0\u4e3a\u6761\u4ef6\u8868\u8fbe\u5f0f\uff0c\u8fd9\u4e9b\u8868\u8fbe\u5f0f\u57fa\u4e8e\u771f(true)\/\u5047 &hellip; <a href=\"https:\/\/www.tansoo.cn\/?p=1465\">\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-1465","post","type-post","status-publish","format-standard","hentry","category-105"],"_links":{"self":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1465","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=1465"}],"version-history":[{"count":0,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1465\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}